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: Marco Tabini <email@hidden>
- Date: Wed, 11 Jul 2012 10:15:48 -0400
On 2012-07-11, at 10:03 AM, Dave DeLong <email@hidden> wrote:
> It sounds like the OP is looking for a model object that uses an NSMutableDictionary as the backing store for its properties, as opposed to individual ivars. You can do this.
>
> You declare all your properties, and you declare a single NSMutableDictionary ivar. All of the properties should be implemented as @dynamic in the .m file.
>
> Since the properties are dynamic, the compiler will not generate implementations for them. You'll have to do that yourself at runtime. Fortunately, if you know what you're doing, that's not too hard.
>
> You'll have to override +resolveInstanceMethod: to figure out what the user is trying to do: get or set. You'll use the selector passed in to derive the key under which you're storing the data in the dictionary. With this information, you construct a block of the appropriate signature, capturing whatever information you need, and turn it into an IMP using imp_implementationWithBlock(). You can then add that method to the class using class_addMethod().
>
Another way would be to override the invocation forwarding mechanism (-forwardInvocation: and -methodSignatureForSelector:) to funnel all the messages through a single getter/setter; this would (possibly) mean that less code is generated at runtime, perhaps at the expense of some added complexity and performance degradation.
I think, however, that the best solution here is to just adopt a newer compiler; recent versions of LLVM are capable of automatically synthesizing properties[1], and provide support for both literals and boxed expressions[2], which make initializing and using both NSArray and NSDictionary much closer to what the OP was looking for.
—Mt.
[1]: “Objective-C Autosynthesis of Properties” in http://clang.llvm.org/docs/LanguageExtensions.html#objc_lambdas
[2]: http://clang.llvm.org/docs/ObjectiveCLiterals.html
_______________________________________________
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