【问题标题】:UILocalNotification crashUILocalNotification 崩溃
【发布时间】:2011-01-09 01:25:03
【问题描述】:

你能帮帮我吗?

我正在设置一个 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)'

有什么想法吗?

【问题讨论】:

    标签: iphone dictionary


    【解决方案1】:

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

    【讨论】:

    • 您能否提供一个您提到的NSKeyedArchiver 方法的示例?
    【解决方案2】:

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

    【讨论】:

    • 实际上,我在 fetchedObjects 中有对象,我想做的就是从名为 textbody 的键中检索一个值。但我真的不知道如何做到这一点。我实际上想做的是为 fetchedObjects 中包含的每个对象创建一个 UILocalNotification,其中包含 textbody 的值(顺便说一句,这是一个字符串)。
    • 我只是个菜鸟,不知道该怎么做,尽管我绞尽脑汁研究 NSDictionaries 的类参考。如果有人可以提供帮助,将不胜感激。
    • 尝试 [fetchedObjects valueForKeyPath:@"textbody"] 你会得到一个 NSArray,其中包含对象的所有 textbody 属性。
    • 感谢您的帮助,这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2022-11-02
    • 1970-01-01
    • 2011-11-22
    • 2021-05-14
    相关资源
    最近更新 更多