3

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 ?

Robin
  • 10,011
  • 5
  • 49
  • 75
Miky Mike
  • 341
  • 6
  • 17

2 Answers2

10

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.

mgile
  • 376
  • 2
  • 3
7

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.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • Actually, I have objects in fetchedObjects and all I want to do is retrieve a value from a key that is called textbody. But I'm really at a loss regarding how to do that. What I would like to do actually is to create a UILocalNotification containing the value of textbody (which is a string by the way)for each of the objects contained in fetchedObjects. – Miky Mike Jan 09 '11 at 11:18
  • I'm just a noob and can't figure out how to do it though I'm racking my brains over the class reference of NSDictionaries. If someone could help, that would be much appreciated. – Miky Mike Jan 09 '11 at 11:27
  • Try [fetchedObjects valueForKeyPath:@"textbody"] you get a NSArray containing all the textbody properties of your objects. – Jilouc Jan 09 '11 at 21:59