Re: How to implement an object whose properties are really dictionary entries.
Re: How to implement an object whose properties are really dictionary entries.
- Subject: Re: How to implement an object whose properties are really dictionary entries.
- From: Keary Suska <email@hidden>
- Date: Wed, 11 Jul 2012 21:26:18 -0600
On Jul 11, 2012, at 6:53 PM, Dave DeLong wrote:
>
> On Jul 11, 2012, at 4:54 PM, Keary Suska wrote:
>
>> On Jul 11, 2012, at 2:45 PM, Motti Shneor wrote:
>>> Of what I read from everyone, and after examining the suggested code from Jens Alfke, I think I'm inclined to something simpler, hinted by Keary Suska. Could you spare a few more words on the "undefinedKey" override?
>>
>> I would create a base class that implements -valueForUndefinedKey: and -setValue:forUndefinedKey: (per the doc links that another poster provided). These methods would simply get/set from the dictionary. The only thing that requires a little consideration is how you may choose to validate the key (if at all)--i.e. determine whether you want to throw an exception for unknown keys. You can throw by simply calling super's implementation. To validate you could keep an array of valid key names (somewhat fragile, as you need to keep it updated), or use runtime inspection functions (see: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW24). More cryptic but not fragile.
>
> I'm probably missing something, but why bother with the undefinedKey methods? Why not just override -setValue:forKey: and -valueForKey:?
Good question--for me, it is a matter of extensibility. This way I can specifically handle pseudo-proeprties separate from implemented properties and not have to code for both. A good example would be dynamic or dependent properties that need custom methods. Also, since valueForKey methods are highly optimized, the extra overhead is negligible (in my tests, at least).
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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