1

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.

gamersoul
  • 101
  • 10

1 Answers1

3

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.

Community
  • 1
  • 1
Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
  • however if I write this one line just above the lines I wrote :: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; then it is not nil !! :-/ – gamersoul Jun 21 '12 at 22:23
  • Right on - then you know where to look! It's probably the format you're specifying. You look new around Stack Overflow, so generally what you do now is open a new question if you can't figure out why the `formatter` is returning `nil`. Good luck! – Ash Furrow Jun 21 '12 at 22:23