3

Is there any generic implementation which converts any Object into NSDictionary, sets all variable names as keys and values as dictionary values?

Syed Absar
  • 2,274
  • 1
  • 26
  • 45
  • See this answer to convert objects into NSDictionary [Convert objects into NSDictionary][1] [1]: http://stackoverflow.com/a/12890495/600076 – iHS Oct 15 '12 at 06:53
  • 1
    This is the best dynamic way I found http://stackoverflow.com/questions/10318366/obj-c-easy-method-to-convert-from-nsobject-with-properties-to-nsdictionary#10318659 – Khaled Annajar Mar 31 '13 at 13:29

4 Answers4

4

In order to achieve your objective, you can use Key-Value Coding. This protocol provides a mechanism to set values of object properties based on the names of the properties represented as NSString's rather than calling the accessors directly.

In order for it to work, you need to have defined your objects with accessors that follow the naming conventions (easy enough using properties). You can see the NSKeyValueCoding protocol guide here:

http://bit.ly/es6kyH

And the Key-Value Coding programming guide here:

http://bit.ly/fBY3Qa

You'll still have to do the iteration, but it's a good start.

z00b
  • 356
  • 2
  • 3
3

Solved using SBJSONParser, converted NSObject to JSON Representation and then fetched NSDictionary out of it.

Syed Absar
  • 2,274
  • 1
  • 26
  • 45
  • Could you show me an example i am trying to transform an NSobject to json and found no good examples could you give me a link? – Radu Jul 27 '11 at 12:43
  • 1
    http://code.google.com/p/json-framework/source/browse/trunk/Source/NSObject%2BSBJSON.m?r=532 – Syed Absar Jul 28 '11 at 04:13
  • 1
    If you use JSONKit, you would do NSDictionary *deserializedData = [jsonString objectFromJSONString]; – loretoparisi Jul 14 '12 at 00:29
1

The perfect way to do this is by using a library for serialization/deserialization many libraries are available but one i like is JagPropertyConverter https://github.com/jagill/JAGPropertyConverter

it can convert your Custom object into NSDictionary and vice versa

Assuming

@interface Person : NSObject

@property (nonatomic, retain) NSNumber * age
@property (nonatomic, retain) NSString * address
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * cellNo;

@end

JAGPropertyConverter *converter = [[JAGPropertyConverter alloc]init];
converter.classesToConvert = [NSSet setWithObjects:[Person class], nil];

Person *person = [[Person alloc]init];
person.name = @"name";
person.address = @"address";
person.age = @27;
person.cellNo = @"XXXXXXXXXX";

//For Object to Dictionary 
NSDictionary *dictPerson = [converter convertToDictionary:person];


//For Dictionary to Object 
Person *person = [[Person alloc]init];
[converter setPropertiesOf:person fromDictionary:dictPerson];
M.Shuaib Imran
  • 1,287
  • 16
  • 29
0

No, you could have everything in your class, e.g. references to other objects and primitives. NS-Dictionary can serialize itself to a NSString and NSString can recreate the dictionary from the string. Best will be you supply your own methods to serialize it.

Helge Becker
  • 3,219
  • 1
  • 20
  • 33