UILocalNotification crash - iphone

could you please help me ?
I'm setting up an UILocalNotification and It crashes when I try to set its userInfo dictionary. fetchedObjects contains 88 objects.
Here is the code :
NSDictionary* myUserInfo = [NSDictionary dictionaryWithObject: fetchedObjects forKey: #"textbody"];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
// défining the interval
NSTimeInterval oneMinute = 60;
localNotif.timeZone = [NSTimeZone localTimeZone];
NSDate *fireDate = [[NSDate alloc]initWithTimeIntervalSinceNow:oneMinute];
localNotif.fireDate = fireDate;
localNotif.userInfo = myUserInfo; //this is the line that crashes the app
[fetchedObjects release];
and the console gives me this :
Property list invalid for format: 200
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unable to serialize userInfo: (null)'
Any idea ?

Actually, even if using an NSCoding-compliant object, UILocalNotification will throw an NSInvalidArgumentException upon calling setUserInfo. The UILocalNotification apparently keeps a more stringent interpretation of property-list types, in which only the stock objects specified in the Property List Programming Guide are allowed. You can get around this by using NSKeyedArchiver to serialize your custom NSCoding-compliant object to an NSData instance, which may be safely passed to UILocalNotification in the userInfo dictionary.

Sounds like there are objects in your userInfo dictionary that don't implement the NSCoding protocol. Everything in that dictionary has to be able to be written to 'disk', since your app may not be running when the notification fires. If there's something in there that can't be serialized, this is the result.

Related

Unrecognized selector error processing results from geocodeAddressString

I'm trying to create multiple placemarks using MKMapItem without using coordinates.
I used location name directly in geocodeAdressString:#"Mumbai"... but I got result for single location.
While I use multiple locations through array, I'm getting this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0xab48380'
Why is this problem occurring?
Class mapItemClass=[MKMapItem class];
if(mapItemClass &&[mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
NSArray *addr=[[NSArray alloc ]initWithObjects:#"Banglore",#"Mumbai",#"Delhi", nil];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *geocodedPlacemark=[placemarks objectAtIndex:0];
MKPlacemark *placemark=[[MKPlacemark alloc]initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary];
MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
[MKMapItem openMapsWithItems:#[mapItem] launchOptions:nil];
}];
}
The error state that -[__NSArrayI length]: unrecognized selector sent to instance 0xab48380'
NSArray dont have a property length. So it is unable to find the selector of length. So check where you are using NSArray and keep break points to find where error is happening. length is the method of NSString and NSData but NSArray dont have length, it has count
I have been able to replicate this using this code
id array = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSLog(#"%d",[array length]);
output
-[__NSArrayI length]: unrecognized selector sent to instance 0x96bafe0
notice how I have used id instead of NSArray with using id I am able to call length which isn't allowed by NSArray, but this gets round the compiler when using id.
So the best way to find out where this is going wrong is add an exception that will catch all exceptions. Do this by selecting the exceptions tab in the project navigator wind >> select '+' >> 'Add Exception Breakpoint...' >> then just select done. This will set a breakpoint every time your app throws an exception.
EDIT
Thanks to the code you have added I suspect that you are passing an NSArray where there should be an NSString. You create
NSArray *addr=[[NSArray alloc ]initWithObjects:#"Banglore",#"Mumbai",#"Delhi", nil];
then pass it to geocodeAddressString:addr
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
Just from the name of this parameter I suspect it should be an NSString and not an NSArray try replacing addr to [addr objectAtIndex:0] this will get the string object at index 0 of the addr array.
EDIT 2
Here is the method you are calling notice it only allows an NSString to be passed in for geocodeAddressString.
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

how to update NSMutableDictionary

I have
NSMutableDictionary *mutDic;
its loaded with some values from other NSMutableDictionary
from an alert i am trying to update its value
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self.mutDic setValue:[[alertView textFieldAtIndex:0] text] forKey:#"lname"];
}
but i am getting this exception
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
how we can update dictionary ?
I had the same exception before even though my dictionary was mutable. Let me explain my scenario to you, may be it will help :
I had NSMutableArray of NSMutableDictionary,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict = [array objectAtIndex:0];
[dict setObject:#"" forKey:#""]; <-- it was crashing on this line...
so I changed my code as below,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[array objectAtIndex:0]];
it worked fine :)
found out the exception problem, but not solved fully
on load i need to take values from other dictionary for that the method i used was wrong, i just assign oldDic to mutDic, i changed to
self.mutDic = [[[NSMutableDictionary alloc] initWithDictionary:manager.oldDic] retain];
their initialized it by
self.oldDic = [[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"F Name",#"fname",#"L Name",#"lname", nil ]retain];
that solved the exception

Objective-C crash issue: NSInvalidArgumentException

I am stuck in it for a long time, but can not find a solution. Here is my code:`
NSLog(#"[tempArray retainCount]: %d",[tempArray retainCount]);
tempArray = [[NSMutableArray alloc] initWithArray:[allRemainingProductsDictionary objectForKey:[[allRemainingProductsDictionary allKeys]objectAtIndex:counter]]];
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];
[tempDictionary setObject:productName forKey:#"name"];
[tempArray release];
I am getting a NSException crash with this report. Please help.
The method getObjects:range: that is being sent to your NSDictionary instance is a NSArray method.
You're probably trying to to your initWithArray passing a NSDictionary instead of a NSArray.
Is the NSLog entry showing up? If not, it's because tempArray does not respond to retainCount. You don't need to worry about anything with release or retain if you're using Xcode 4.2 with ARC for iOS 5 (which you should, unless you have legacy code).
Otherwise, somewhere you're sending an object a message it doesn't respond to.

iPhone - UILocalNotification fireDate problem

I'm trying to get the fireDate from an already set notification
Heres my code:
NSArray *notificationArray = [[NSArray alloc] initWithObjects:[[UIApplication sharedApplication] scheduledLocalNotifications], nil];
if ([notificationArray count] > 0) {
NSDate *now = [NSDate date];
UILocalNotification *locNotification = [[UILocalNotification alloc] init];
locNotification = [notificationArray objectAtIndex:0];
NSDate *otherDate = locNotification.fireDate;
}
The locNotification has the values but the last line when I try to instantiate otherDate I'm getting
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM fireDate]: unrecognized selector sent to instance 0x1e4e20'
* Call stack at first throw:
I really don't know what I'm doing wrong.
Thanks in advance
You're using the wrong init method. Instead of NSArray's -initWithObjects:, you want:
NSArray *notificationArray = [[ NSArray alloc ] initWithArray:
[[ UIApplication sharedApplication ]
scheduledLocalNotifications ]];
You get the exception because notificationArray only contains one object, the array returned by -scheduledLocalNotifications.

Get EXEC_BAD_ACCESS when I get the NSFileModificationDate

I try to get the last modification date of a file:
NSFileManager *fm = [[NSFileManager alloc] init];
NSError *err;
NSDate *lastModif = [[fm attributesOfItemAtPath:filename error:&err] objectForKey:NSFileModificationDate];//filename is ok ;-)
if(err == nil) {
[lastModif retain];
//I can put a NSLog of lastModif here, it works !!
NSTimeInterval lastModifDiff = [lastModif timeIntervalSinceNow];//crash here
}
I don't understand why the NSDate seems to be released, why the retain does not retain it.
Thank you if you have any idea...
You don't need to retain lastModif. I think you might be trying to treat lastModifDiff as an object of some sort when you do an NSLog with it or whatever you do with it afterwards. NSTimeInterval is a typedef to a double so you need to treat it as a double or [NSNumber numberWithDouble:lastModifDiff] if you want to use it like an object.
I'm having the same problem, but this post seemed germane:
NSDate : timeIntervalSinceNow crash
I'm writing a simple set of functions- startClock/endClock -using NSDate to determine FPS in my game loop. Except that timeIntervalSinceNow crashes, claiming that my earlier set NSDate object doesn't exist.
I know for a fact that the NSDate object has a retain count of 1 when I call startClock, but my theory is that NSDate instances are internally rigged to auto-release when they get bored and aren't feeling useful.
Using retain/release to assume ownership of these flighty and ephemeral NSDate objects worked for me.