2011-01-09 93 views
3

你能帮我吗?UILocalNotification崩溃

我设置了一个UILocalNotification,当我尝试设置它的userInfo字典时,它崩溃。 fetchedObjects包含88个对象。

下面是代码:

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]; 

和控制台给了我这样的:

Property list invalid for format: 200 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unable to serialize userInfo: (null)' 

任何想法?

回答

7

听起来像你的userInfo字典中没有实现NSCoding协议的对象。该字典中的所有内容都必须能够写入“磁盘”,因为当通知触发时,您的应用可能不会运行。如果那里有东西不能被序列化,那就是结果。

+0

实际上,我在fetchedObjects中有对象,我想要做的就是从名为textbody的键中检索一个值。但是我对如何做到这一点感到非常失望。我想要做的实际上是为包含在fetchedObjects中的每个对象创建一个包含textbody值(这是一个字符串)的UILocalNotification。 – 2011-01-09 11:18:37

+0

我只是一个noob,并不知道如何做到这一点,虽然我绞尽脑汁对NSDictionaries的类参考。如果有人可以帮助,那将非常感激。 – 2011-01-09 11:27:08

10

实际上,即使使用符合NSCoding的对象,UILocalNotification在调用setUserInfo时也会引发NSInvalidArgumentException。 UILocalNotification显然保留了对属性​​列表类型的更严格的解释,其中只允许属性列表编程指南中指定的股票对象。您可以通过使用NSKeyedArchiver将您的自定义NSCoding兼容对象序列化为NSData实例来解决此问题,该实例可以安全地传递给userInfo字典中的UILocalNotification。