Is there a reverse "setValuesForKeysWithDictionary" - a makeDictionaryWithObjectProperties?

Viewed 5637

I parse some JSON from a web service, this gives me an NSDictionary, I use this dictionary to populated properties on a valueEntity of type NSObject by

[myObject setValuesForKeysWithDictionary:JSONDict];

(myObject has the same property names and types as the dictionary from the JSON parser)

name = name
count = count
startDate = startDate
etc..

Is there a way to go the other way, where I have an NSDictionary that I would like to have "filled" with the properties and their values from an my NSObject subclass. Like I suggest in the title, something along the lines of this:

one way

MyObject *myObject = [[MyObject alloc] init];
[myObject setValuesForKeysWithDictionary:JSONDict];

the other way around

NSMutableDictionary *dict = [myObject makeDictionaryWithObjectProperties];

The reason for this is that I have a valueEntity which, by protocol, my views all conform to, but I would like to populate an NSManagedObject with the values as well. So I thought that using the NSDictionary as an intermediate step I can get around having to do a category on my NSManagedObject that sets each property manually from the value on my object subclassing NSObject.

With a dictionary I can go:

[myManagedObject setValuesForKeysWithDictionary:dict];

I just can't get the dictionary representation back out once I done the above?

3 Answers
Related