Re: Problem using dictionary
Re: Problem using dictionary
- Subject: Re: Problem using dictionary
- From: Matt Neuburg <email@hidden>
- Date: Sat, 09 Oct 2010 17:22:45 -0700
- Thread-topic: Problem using dictionary
>Date: Fri, 8 Oct 2010 13:47:18 +0200
>From: Remco Poelstra <email@hidden>
>Subject: Re: Problem using dictionary
>
>Ah, I should use valueForKeyPath:. Is there a reason valueForKey: is
>documented directly but valueForKeyPath: is not?
First off, this is an extremely common mistake. At least, it's common in
my life. If I had a dollar for every time I've said valueForKey: when I
should have said valueForKeyPath:, and then wondered why my code wasn't
working, I could afford a large Starbucks cappuccino.
That said, however, this is not a documentation problem but a knowledge
problem. *You* are the one who has evidently set up your dictionary plist
in such a way that you've got a dictionary with a key "page1", whose value
is something (another dictionary, perhaps?) with a key "fwRevCode". And
you must have been aware of this fact, because you yourself formed the
string "page1.fwRevCode" in order to get at it. So it's your own fault
that you claimed that this was a key when in fact it was a keypath. You
knew it was a keypath when you formed it; else, what's the dot for?
In other words, this has nothing whatever to do with NSDictionary. If
you're staring at the NSDictionary docs wondering where the documentation
for this is, you're staring in the wrong place. It has to do with KVC. If
this really is a dictionary in a dictionary, you could equally well have
chained two calls to objectForKey: and gotten at your data without using
KVC at all. That's how you get at a key's value with an NSDictionary. But
that isn't what you elected to do. You elected to use KVC; so it's up to
you to know KVC and use it properly.
m.
>
>Op 8 okt 2010, om 13:34 heeft Remco Poelstra het volgende geschreven:
>
>>Hi,
>>I've the following code:
>>NSDictionary *dict=[[NSDictionary alloc]
>>initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Indexes"
>>ofType:@"plist"]]; //This is actually a global initialized in
>>+initialize.
>>NSString *key=[NSString stringWithFormat:@"%@.%@",page,property];
>>NSLog(@"%@",key);
>>NSLog(@"%@",[[dict valueForKey:key] description]);
>>NSLog(@"%@",[[[dict valueForKey:page] valueForKey:property]
>>description]);
>>The result from the code is:
>>page1.fwRevCode
>>(null)
>>6
>>The first and last are exactly what I expect. But why can't the
>>dictionary find the value with the full key?
>>
--
matt neuburg, phd = email@hidden, http://www.tidbits.com/matt/
pantes anthropoi tou eidenai oregontai phusei
Among the 2007 MacTech Top 25, http://tinyurl.com/2rh4pf
AppleScript: the Definitive Guide, 2nd edition
http://www.tidbits.com/matt/default.html#applescriptthings
Take Control of Exploring & Customizing Snow Leopard
http://tinyurl.com/kufyy8
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden