Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 854
theMikeSwan Avatar asked Apr 18 '10 04:04

theMikeSwan


4 Answers

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.
like image 157
TechZen Avatar answered Nov 12 '22 22:11

TechZen


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.

like image 44
Peter Hosey Avatar answered Nov 12 '22 23:11

Peter Hosey


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]);
    }
}
like image 44
Tico Ballagas Avatar answered Nov 12 '22 23:11

Tico Ballagas


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

like image 39
Micha Avatar answered Nov 12 '22 22:11

Micha