4

I have an error when trying to run the application on my iPhone. I don't understand why I have a nil error in this case

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

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?

Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34
Siberian Scout
  • 45
  • 1
  • 1
  • 5

2 Answers2

5

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"];
Manvik
  • 977
  • 14
  • 26
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
1

You cannot put nil values into a dictionary. 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? That's something only you can decide. Mukesh's answer should you how to store an empty string instead of 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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Thanks for your answer. I'm still looking to improve this part but this answer helped me to check if there were other issues. – Siberian Scout Dec 02 '15 at 11:32