How to implement an object whose properties are really dictionary entries.
How to implement an object whose properties are really dictionary entries.
- Subject: How to implement an object whose properties are really dictionary entries.
- From: Motti Shneor <email@hidden>
- Date: Wed, 11 Jul 2012 15:34:38 +0300
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?
Motti Shneor,
Spectrum Reflections Ltd.
_______________________________________________
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