What will cause objectForKey: return null with a valid string?

I am having a problem with NSDictionary returning null for an NSString , although 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 instructions:

 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 , but when I call objectForKey: I get null . I even tried a backup project cleanup option.

Has anyone ever come across this, or did I just forget something really basic?

+7
objective-c cocoa nsstring nsdictionary
source share
6 answers

At the moment, you still have a reporting error from DLog for some reason.

Try:

  • Record in NSLog.
  • Check the value of newSourceString directly in the debugger while the code is alive.
+3
source share

What would cause objectForKey: return null with a valid string?

One of two things:

  • The dictionary does not contain an object for this key. (Do you think this is so does not matter.)
  • You do not have a dictionary; aDict is nil , so you send an objectForKey: message to nil . Messages in nil do nothing but return nil .

As you can see, the string is in the dictionary under the key newSource ...

Actually, I'm not sure what is going on there. An NSDictionary description (if it contains a string for this key) would be { newSource = "some string here"; } { newSource = "some string here"; } , which does not match the description that you registered. On the other hand, if it were an object that is not a dictionary, you should get a "does not respond to selector" exception when trying to send an objectForKey: message to it. Therefore, while it appears, from your output log file, to be something, I have no idea what it is, except that it is probably not a dictionary.

It's just weird then.

+2
source share

I had a similar problem. For me, the problem was that I thought my key was NSString when it was actually NSNumber. You can verify your key using the following

 for (id key in [aDict allKeys]){ NSLog(@"%@:%@",key, [[key class] description]); } } 
+2
source share

You cannot ignore the environment in which you are coding. If it is with GNUstep, in particular gnustep1.19, read on. Otherwise, ignore.

I just ran into a very strange error with gnustep1.19 (.3), but it imitates 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, the “key” is initialized by extracting it from another NSDictionary, and some entries in another dictionary (loaded from the file) contain Unicode characters. This is so far the only correlation I have found - removing Unicode from the source file and re-executing the application makes it work.

This is not a problem for gnustep1.18 or> = gnustep1.20

+1
source share

I suspect that your string is actually literally "(null)"), that is, it lasts 6 letters and is called (-null-).

0
source share

I suspect that aDict is not an instance of NSDictionary. register it for confirmation.

0
source share

All Articles