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 07:46:16 -0600
On Jul 11, 2012, at 6:34 AM, Motti Shneor wrote:
> Hi. I guess many have already stumped into this one, but my wildest phrasing attempts in web-searches didn't yield.
>
> I'd like to have a "data" object, similar to (but much simpler than) CoreData entity, which only exports a bunch of properties. Something like
>
> @interface MSPersonalData :NSObject
> {
> }
> -(id) initWithData:(NSDictionary*)initialData;
> -(id) init;
> @property (nonatomic, retain) NSString* firstName;
> @property (nonatomic, retain) NSString* lastName;
> @property (nonatomic, retain) NSDate* birthDate;
> @property (nonatomic, retain) NSNumber* salary;
> @end
>
> I'd like this object to have NO ivars, and NO property implementations, not even synthesized. Instead I'd like the object to either inherit from NSMutableDictionary or own a single NSMutableDictionary ivar, and I'd like any access to my object to be transparently translated to internal dictionary valueForKey: and setValue:forKey:.
>
> Simply put, I'd like code like this:
>
> MSPersonalData *myDataObject = ;[MSPersonalData alloc] init];
> myDataObject.firstName = @"Motti";
>
> to actually perform
>
> [self setValue:@"Motti" forKey: @"firstName"]; // if MSPersonalData inherits NSMutableDictionary
> or
> [internalDictionary setValue:@"Motti" forKey: @"firstName"]; // if MSPersonalData owns NSMutableDictionary.
>
> Such object can be very useful for UI binding, and as self-documented API for a sub-system. Instead of declaring a set of static keys as string, just behave as if there were iVars per each key of the object. I think CoreData NSManagedObjects do this, in their "primitive" implementation.
>
> I tried several schemes for implementation, but all became cumbersome, and I had to do much trickery, and in the end it looked ugly. I strongly feel there must be a simple and elegant way to do this. To have a mutable dictionary react to the "syntactic sugar" of dot notation.
>
> Is there a good, general solution for this?
What is hampering you, it seems to me, is your requirement to use dot syntax. At least, to use dot syntax and not get a bunch of compiler warnings. Even Core Data still requires you to declare properties to avoid compiler warnings, which is a good sign that what you are after is not "cleanly" possible.
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