Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an object into NSDictionary

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

like image 362
Syed Absar Avatar asked Dec 08 '25 23:12

Syed Absar


2 Answers

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.

like image 165
z00b Avatar answered Dec 12 '25 01:12

z00b


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

like image 44
Syed Absar Avatar answered Dec 11 '25 23:12

Syed Absar