gamersoul
gamersoul

Reputation: 101

Error in NSDictionary

I am using the following line of code to create a dictionary which stores a url and the time when it was accessed::

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: urlString, [formatter stringFromDate:[NSDate date]], nil];

But,however, I am getting the following error :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil.  Or, did you forget to nil-terminate your parameter list?'

Can someone help me to sort it out ?? I am stuck at this error. Thanks and regards.

Upvotes: 1

Views: 1784

Answers (1)

Ash Furrow
Ash Furrow

Reputation: 12421

NSDateFormatter is probably returning nil. Check the return value, store it in a local variable, and add that variable to the dictionary instead.

NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSLog(@"dateString: %@", dateString); //will let you know if it's nil or not
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: urlString, dateString, nil];

You can check out why it might be returning nil here.

Upvotes: 3

Related Questions