views:

211

answers:

4

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 something really basic?

+1  A: 

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

One of two things:

  1. The dictionary does not contain an object for that key. (Whether you think it does is irrelevant.)
  2. You don't have a dictionary; aDict is nil, so you're sending the objectForKey: message to nil. Messages to 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'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.

Peter Hosey
Every time I put in the curly braces they screwed up the code block so I had to delete them (still not sure why that was happening).
theMikeSwan
I've updated the question to include the curly braces in the log statement based on @theMikeSwan's comment above.
Steve Harrison
A: 

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-).

Andy Milburn
The first line of the print out shows that the key does have a value of "Second View".
TechZen
If that were the case, then the step that applies `stringByAppendingString:` should not still produce `(null)`.
Isaac
+1  A: 

At this point, you're left with a reporting error from DLog for some reason.

Try:

  1. Logging with NSLog.
  2. Check the value of newSourceString directly in the debugger while the code is live.
TechZen
A: 

Hi,

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

Micha