【问题标题】:'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: device_uid)''NSInvalidArgumentException',原因:'*** setObjectForKey:对象不能为 nil(键:device_uid)'
【发布时间】:2016-03-06 11:35:24
【问题描述】:

尝试在 iPhone 上运行应用程序时出现错误。我不明白为什么在这种情况下我有一个 nil 错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '  setObjectForKey: object cannot be nil (key: device_id)'
First throw call stack: (0x183ce0f48 0x19918bf80 0x183bcc4e0 0x10006f5f0 0x1002b9ca8 0x1002b9c68 0x1002bf710 0x183c981f8 0x183c96060 0x183bc4ca0 0x18f148088 0x1892dcffc 0x1000716a0 0x1999ce8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是 appdelegate.m 文件中与此错误有关的部分:

// Get the users Device Model, Display Name, Token & Version Number
UIDevice *device = [UIDevice currentDevice];

NSUserDefaults *dict = [NSUserDefaults standardUserDefaults];
NSString *identifier = [dict stringForKey:@"identifier"];
NSString *deviceName = device.name;
NSString *deviceModel = device.model;
NSString *deviceSystemVersion = device.systemVersion;

// Prepare the Device Token for Registration (remove spaces and < >)
NSString *deviceToken = [[[[token description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];

NSMutableDictionary *postDatas = [NSMutableDictionary dictionary];
[postDatas setObject:appName forKey:@"app_name"];
[postDatas setObject:appVersion forKey:@"app_version"];
[postDatas setObject:identifier forKey:@"device_uid"];
[postDatas setObject:deviceToken forKey:@"device_token"];
[postDatas setObject:deviceName forKey:@"device_name"];
[postDatas setObject:deviceModel forKey:@"device_model"];
[postDatas setObject:deviceSystemVersion forKey:@"device_version"];
[postDatas setObject:pushBadge forKey:@"push_badge"];
[postDatas setObject:pushAlert forKey:@"push_alert"];
[postDatas setObject:pushSound forKey:@"push_sound"];

Request *request = [Request alloc];
request.delegate = self;

[request postDatas:postDatas withUrl:@"push/iphone/registerdevice/"];

此方法是否已弃用?

【问题讨论】:

    标签: ios objective-c crash ios8.4


    【解决方案1】:

    标识符中的值为零。所以请像这样检查它

    NSString *identifier = [dict stringForKey:@"identifier"];
    
    if([identifier length] != 0)
        [postDatas setObject:identifier forKey:@"device_uid"];
    else
        [postDatas setObject:@"" forKey:@"device_uid"];
    

    【讨论】:

    • 感谢您的回答。不幸的是它不起作用,错误仍然在这里
    • 完全一样。这就是为什么这个错误让我发疯:)
    • @SiberianScout 用我的代码代替这一行 [postDatas setObject:identifier forKey:@"device_uid"];
    • 从这个答案中删除 else 部分,您的应用应该不会崩溃。
    • 实际上错误消息的小变化:原因:'*** setObjectForKey: object cannot be nil (key:)'
    【解决方案2】:

    您不能将 nil 值放入字典中。它会崩溃。

    您真正需要调查的内容是:这些 nil 值从何而来,当您获得 nil 值时应该做什么?那是只有你可以决定的事情。 Mukesh 的回答应该你如何存储一个空字符串而不是 nil。这样可以避免崩溃。 通常是正确的做法,但并非总是如此。因此,如果其中一个值为 nil,请调查您的应用程序应该做什么,并使其完全做到这一点。

    【讨论】:

    • 感谢您的回答。我仍在寻求改进这部分,但这个答案帮助我检查是否还有其他问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多