I have an NSDictionary and I am trying to pull a string out of it. For some reason, the last string seems irretrievable(!?!). In the code below, I retrieve the NSString object for labelString, with no problem at all. But when I try to retrieve the NSString for foo, I always get nil. But I don’t see the difference – can you see what I’m doing wrong?
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:CellStyleLabelledStringCell], @"cellStyle",
@"name", @"fieldName",
@"Name", @"labelString",
foodItem.name, @"contentString",
@"foo", @"fookey",
nil];
NSString *string1 = (NSString *)[dict objectForKey:@"fookey"];
NSString *string2 = (NSString *)[dict objectForKey:@"labelString"];
NSLog(@"[%@][%@]", string1, string2);
The log message looks like this, and backs-up what I’m seeing in the debugger (i.e., string1 is null):
2012-03-17 21:35:03.302 QuickList7[8244:fb03] [(null)][Name]
Truly perplexed. Thanks in advance.
foodItem.nameis nil, so-[NSDictionary dictionaryWithObjectsAndKeys:]stops there, and doesn’t add the subsequent objects to the dictionary.In other words, it’s as if you did this:
This is why you have to be careful with any method that takes
nilas an “end of arguments” sentinel.