请你帮助我好吗 ?
我正在设置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];
Run Code Online (Sandbox Code Playgroud)
而控制台给了我这个:
Property list invalid for format: 200
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unable to serialize userInfo: (null)'
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
小智 10
实际上,即使使用符合NSCoding的对象,UILocalNotification也会在调用setUserInfo时抛出NSInvalidArgumentException.UILocalNotification显然对属性列表类型进行了更严格的解释,其中只允许在"属性列表编程指南"中指定的库存对象.您可以通过使用NSKeyedArchiver将自定义NSCoding兼容对象序列化为NSData实例来解决此问题,该实例可以安全地传递到userInfo字典中的UILocalNotification.
听起来你的userInfo字典中有些对象没有实现NSCoding协议.该字典中的所有内容都必须能够写入"磁盘",因为通知触发时您的应用可能无法运行.如果那里有一些无法序列化的东西,那就是结果.