A category of NSObject that allows you to create objects from dictionary.
- Add NSObject+BITObject folder into your project.
- Import NSObject+BITObject.h.
- Start using the only method available for it.
Say you have a dictionary that if you output to the console looks like this:
{
superheros = (
{
age = 35;
isDead = 1;
name = Batman;
powers = (
{
elements = (
metal,
air
);
name = batmobile;
},
{
elements = (
metal
);
name = batarang;
}
);
},
{
age = 30;
isDead = 0;
name = Superman;
powers = (
{
elements = (
fire,
metal
);
name = "laser eye";
},
{
elements = (
air
);
name = fly;
}
);
}
);
}Before we can convert this dictionary into an object, we'll need to create classes and properties that correlate to the dictionary keys.
First, we need a base object that contains an array of superheros. So, we create a class called, for example, JusticeLeague with an array property that will contain the superheros.
@interface JusticeLeague : NSObject
@property (strong, nonatomic) NSArray *superheros;
@endThen, the Superhero class.
@interface Superhero : NSObject
@property (strong, nonatomic) NSString *name;
@property (unsafe_unretained, nonatomic) NSInteger age;
@property (unsafe_unretained, nonatomic) BOOL isDead;
@property (strong, nonatomic) NSArray *powers;
@endFinally, the Power class.
@interface Power : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSArray *elements;
@endNow, we can start using it. First, we allocate and initialize an instance of JusticeLeague. Then, we call the setPropertyValuesWithDictionary method. And voila...we have ourselves a JusticeLeague object with all properties created from the dictionary values.
JusticeLeague *league = [[[JusticeLeague alloc] init] autorelease];
[league setPropertyValuesWithDictionary:dictionary ignoreMissingPropertyNames:YES];
NSLog(@"%@", [[[league superheros] objectAtIndex:0] name]); //This will be "Batman"