This is a category of NSObject to provide methods to inject a given NSDictionary based data source, e.g. coming from a JSON dictionary, into the object or to get the NSDictionary data source that is representing the current set of properties of an object.
The set of properties to inject/to read are not determined automatically, but have to be specified using a dictionary or an array.
Say we have a JSON result coming from the server:
{
"first-name": "John",
"last-name": "Appleseed"
}
and we have a Person object with the two NSString properties fastName and lastName. Instead of cycling through the entries of our JSON NSDictionary, and manually setting the properties using the synthesized setter methods, let's just inject the NSDictionary by providing both the data and a mapping dictionary:
[person inject:JSON usingMapping:@{
@"first-name": @"firstName",
@"last-name": @"lastName"
}];
That's it.
// assuming myObject has properties NSString *firstName, *lastName;
NSDictionary *data = @{@"first-name":@"John",@"last-name":@"Appleseed"};
NSDictionary *mapping = @{@"first-name":@"firstName", @last-name":@"lastName"};
[myObject inject:data usingMapping:mapping];
// assuming myObject has properties NSString *firstName, *lastName;
NSDictionary *data = @{@"firstName":@"John",@"lastName":@"Appleseed"};
NSArray *list = @[@"firstName", @"lastName"];
[myObject inject:data usingList:list];
// assuming myObject has properties NSString *firstName, *lastName;
NSDictionary *mapping = @{@"first-name":@"firstName", @last-name":@"lastName"};
NSDictionary *representation = [myObject dictionaryRepresentationUsingMapping:mapping];
// assuming myObject has properties NSString *firstName, *lastName;
NSArray *list = @[@"firstName", @"lastName"];
NSDictionary *representation = [myObject dictionaryRepresentationUsingList:list];
- Raffael Hannemann
- @raffael-me
- http://www.raffael.me/
Copyright (c) 2013 Raffael Hannemann Under BSD License.
Follow @raffael-me for similar releases.