Re: The joys of people using valueForKey to get objects out of a dictionary.
Re: The joys of people using valueForKey to get objects out of a dictionary.
- Subject: Re: The joys of people using valueForKey to get objects out of a dictionary.
- From: Jens Alfke <email@hidden>
- Date: Tue, 10 Nov 2015 11:15:54 -0800
> On Nov 10, 2015, at 10:52 AM, Alex Zavatone <email@hidden> wrote:
>
> Here's a great reason to use objectForKey: wherever possible instead of valueForKey: - valueForKey: with an unknown key will throw NSUnknownKeyException saying "this class is not key value coding-compliant for the key ".
Yes, but that’s a non-issue with NSDictionary because there are no unknown keys. It handle any key (that doesn’t start with an @) by looking it up in the dictionary.
I’m not saying you should use -valueForKey, just that you need to be clear on its behavior on _arbitrary_ objects vs. its behavior with NSDictionary.
> But if we remove the @ (obviously unintended), valueForKey causes an EXC_BAD_ACCES while objectForKey reports "error: string literal must be prefixed by '@'".
Um … you’re doing that in lldb? The difference there seems like an lldb bug. It shouldn’t be allowing you to pass a C string literal as an id, no matter what method is being called.
> And David does state that whether an exception is thrown or not is implementation dependent.
Yes, the implementation OF THE CLASS BEING CALLED. Which is NSDictionary. Which, as we keep saying, handles any key by looking it up in the dictionary.
—Jens
_______________________________________________
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