What would cause objectForKey: to return null with a valid string in place?
I am having an issue with NSDictionary
returning null
for an NSString
even though the string is in the dictionary. Here is the code:
- (void)sourceDidChange:(NSNotification *)aNote {
NSDictionary *aDict = [aNote userInfo];
DLog(@"%@", aDict);
NSString *newSourceString = [aDict objectForKey:@"newSource"];
DLog(@"%@", newSourceString);
newSourceString = [newSourceString stringByReplacingOccurrencesOfString:@" " withString:@""];
DLog(@"%@", newSourceString);
NSString *inspectorString = [newSourceString stringByAppendingString:@"InspectorController"];
DLog(@"%@", inspectorString);
newSourceString = [newSourceString stringByAppendingString:@"ViewController"];
DLog(@"%@", newSourceString);
}
And I get the following log statements:
2010-04-17 23:50:13.913 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] { newSource = "Second View"; }
2010-04-17 23:50:13.914 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.916 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.917 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.917 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
As you can see, the string is in the dictionary under the key newSource
, yet when I call objectForKey:
, I get null
. I have even tried the fallback option of cleaning the project.
Has anyone ever run into this, or have I just forgotten some开发者_开发技巧thing really basic?
At this point, you're left with a reporting error from DLog for some reason.
Try:
- Logging with NSLog.
- Check the value of
newSourceString
directly in the debugger while the code is live.
What would cause
objectForKey:
to return null with a valid string in place?
One of two things:
- The dictionary does not contain an object for that key. (Whether you think it does is irrelevant.)
- You don't have a dictionary;
aDict
isnil
, so you're sending theobjectForKey:
message tonil
. Messages tonil
do nothing but returnnil
.
As you can see the string is in the dictionary under the key newSource…
Actually, I'm not sure what's going on there. An NSDictionary's description (if it contained a string for that key) would be { newSource = "some string here"; }
, which doesn't match the description you logged. On the other hand, if it were an object that isn't a dictionary, you should get a “does not respond to selector” exception upon trying to send it an objectForKey:
message. So while it does appear, from your log output, to be something, I have no idea what it is, except that it is probably not a dictionary.
That's just plain strange, then.
I ran into a similar issue. For me the problem was that I thought my key was a NSString when it was actually a NSNumber. You can check your key using the following
for (id key in [aDict allKeys]){
NSLog(@"%@:%@",key, [[key class] description]);
}
}
You fail to neglect the environment in which you're coding. If it's with GNUstep, specifically, gnustep1.19, read on. Otherwise ignore.
I just encountered a very odd bug with gnustep1.19(.3) but it mimics this question perfectly.
NSString * key = <some string>
NSDictionary * dict = <some dictionary>
(gdb) p [dict objectForKey:key]
$20 = (struct objc_object *) 0x0
(gdb) p [dict objectForKey:@"MyKeyValue"]
$22 = (struct objc_object *) 0x7fffd94fe690
(gdb) p [key compare"@MyKeyValue"]
$25 = NSOrderedSame
In this case, 'key' was being initialised by extracting it from another NSDictionary, and some of the entries in the other dictionary (loaded from a file) contain Unicode characters. That is, so far, the only correlation I have found - removing the unicode from the source file and re-running the app makes it work.
This is not an issue for gnustep1.18 or >=gnustep1.20
I suspect that your string is actually, literally "(null)" -- that is to say, it is 6-letters long, and spells out (-n-u-l-l-).
I suspect that aDict is not an instance of NSDictionary. log it's class to confirm.
精彩评论