简体   繁体   English

'NSInvalidArgumentException',原因:'*** setObjectForKey:对象不能为零(键:device_uid)

[英]'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: device_uid)'

I have an error when trying to run the application on my iPhone. 尝试在iPhone上运行应用程序时出现错误。 I don't understand why I have a nil error in this case 我不明白为什么我在这种情况下没有错误

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

Here is the part of    appdelegate.m file concerned by this error :

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

is this method deprecated? 这个方法被弃用了吗?

You are getting nil in the identifier. 您在标识符中得到零。 So please check it like this 所以请这样检查

NSString *identifier = [dict stringForKey:@"identifier"];

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

You cannot put nil values into a dictionary. 您不能将nil值放入字典中。 It will crash. 它会崩溃。

What you really need to investigate: Where do these nil values come from, and what is the appropriate thing to do when you get nil values? 您真正需要研究的是:这些nil值从何而来,当您获得nil值时该怎么做? That's something only you can decide. 那是只有您可以决定的事情。 Mukesh's answer should you how to store an empty string instead of nil. Mukesh的答案应该是如何存储空字符串而不是nil。 This avoids the crash. 这样可以避免崩溃。 It often is the correct thing to do, but not always. 通常是正确的做法,但并非总是如此。 So investigate what your app should do if one of the values is nil, and make it do exactly that. 因此,请调查您的应用程序在其中一个值为nil的情况下应该做什么,并使其准确地执行该操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView insertSections 错误 - NSInvalidArgumentException 原因 setObjectForKey 对象不能为零 UICollectionViewItemKey - UICollectionView insertSections error - NSInvalidArgumentException reason setObjectForKey object cannot be nil UICollectionViewItemKey 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:对象不能为nil(键:索引)” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: index)' 修复NSInvalidArgumentException:*** setObjectForKey:对象不能为nil(键:updated_objectIDs) - Fix a NSInvalidArgumentException: *** setObjectForKey: object cannot be nil (key: updated_objectIDs) 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:键不能为零” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil' 使用原因setObjectForKey切换uitextfields时iOS 7.1更新崩溃:对象不能为nil(键:NSShadow) - iOS 7.1 update crash when switching between uitextfields with reason setObjectForKey: object cannot be nil (key: NSShadow) setObjectForKey:exception:object不能为nil - setObjectForKey: exception: object cannot be nil 间歇性崩溃:-setObjectForKey:对象不能为零(键:ref_id) - Intermittent Crash: - setObjectForKey: Object cannot be nil (key:ref_id) App Crash setObjectForKey:对象不能为nil - App Crash setObjectForKey: object cannot be nil setObjectForKey:对象无法在iPhone应用程序中崩溃 - setObjectForKey: object cannot be nil crash in iphone app Coredata错误setObjectForKey:对象不能为零 - Coredata error setObjectForKey: object cannot be nil
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM