IOS NSDictionary and Model
This tutorial describes how to extend NSObject through runtime and assign values to the Model using dictionaries. This is a very useful technique.
Runtime details will be detailed later
Source code:
NSObject + Property. h and NSObject + Property. m
/// NSObject + Property. h // ModelValueTest1.0 /// Created by Lisa on 14-9-15. // Copyright (c) 2014 Lisa. all rights reserved. // # import
@ Interface NSObject (Property)-(void) setDataDictionary :( NSDictionary *) dataDictionary;-(NSDictionary *) dataDictionary; @ end
/// NSObject + Property. m // ModelValueTest1.0 /// Created by Lisa on 14-9-15. // Copyright (c) 2014 Lisa. all rights reserved. // # import NSObject + Property. h # import
@ Implementation NSObject (Property) # pragma mark -- Public method-(void) setDataDictionary :( NSDictionary *) dataDictionary {[self setAttributes: dataDictionary obj: self];}-(NSDictionary *) dataDictionary {// obtain the attribute list NSArray * properties = [self propertyNames: [self class]; // obtain the attribute value based on the attribute list return [self propertiesAndValuesDictionary: self properties: properties];} # pragma mark --- Private method // use the attribute name to piece together the setter method-(SEL) getSetterSelWithAttibuteName :( NSString *) attributeName {NSString * captial = [[attributeName substringToIndex: 1] uppercaseString]; NSString * setterSelStr = [NSString stringWithFormat: @ set % @:, captial, [attributeName substringFromIndex: 1]; return NSSelectorFromString (setterSelStr );} // set attributes through the dictionary-(void) setAttributes :( NSDictionary *) dataDic obj :( id) obj {// obtain all key values NSEnumerator * keyEnum = [dataDic keyEnumerator]; // The dictionary key value (one-to-one correspondence with the model attribute value) id attrbuteName = nil; while (attrbuteName = [keyEnum nextObject]) {// get the assembled setter method SEL sel = [obj getSetterSelWithAttibuteName: attrbuteName]; // verify whether the setter method can respond to if ([obj respondsToSelector: sel]) {id value = nil; id tmpValue = dataDic [attrbuteName]; if ([tmpValue isKindOfClass: [NSNull class]) {// if it is of the NSNull type, then the value is null value = nil;} else {value = tmpValue;} // execute the setter method [obj %mselec%mainthread: sel withObject: value waitUntilDone: [NSThread isMainThread] ;}}// obtain the attribute name list of a Class-(NSArray *) propertyNames :( Class) class {NSMutableArray * propertyNames = [[NSMutableArray alloc] init]; unsigned int propertyCount = 0; optional * properties = class_copyPropertyList (class, & propertyCount); for (unsigned int I = 0; I
Tested model
LModelItems. h and LModelItems. m
/// LModelItems. h // ModelValueTest1.0 /// Created by Lisa on 14-9-15. // Copyright (c) 2014 Lisa. all rights reserved. // # import
@ Interface LModelItems: NSObject @ property (nonatomic, strong) NSString * name; @ property (nonatomic, strong) NSNumber * age; @ property (nonatomic, strong) NSDictionary * addressDic; @ property (nonatomic, strong) NSArray * eventd; @ end
/// LModelItems. m // ModelValueTest1.0 /// Created by Lisa on 14-9-15. // Copyright (c) 2014 Lisa. all rights reserved. // # import LModelItems. h @ implementation LModelItems @ end
Use Cases:
In the controller, choose >>>> LViewController. h and LViewController. m.
// LViewController. h // ModelValueTest1.0 // Created by Lisa on 14-9-15. // Copyright (c) 2014 Lisa. All rights reserved. // # import
@ Interface LViewController: UIViewController @ end
/// LViewController. m // ModelValueTest1.0 /// Created by itotem on 14-9-15. // Copyright (c) 2014 Lisa. all rights reserved. // # import LViewController. h # import NSObject + Property. h # import LModelItems. h @ interface LViewController () @ end @ implementation LViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: role]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; LModelItems * model = [LModelItems new]; // assign a value to the model through the dictionary. dataDictionary =@ {@ name: @ Lisa, @ age: @ 26, @ addressDic :{ {@ provinces: @ Beijing, @ area: @ Haidian District}, @ eventd: @ [@ first, @ second, @ third]}; // print the value assignment result NSLog (% @, model. dataDictionary); NSLog (@ name =%@, model. name); NSLog (@ age =%@, model. age); NSLog (@ addressDic =%@, [model. addressDic objectForKey: @ provinces]); NSLog (@ eventd =%@, model. eventd);}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
Print Information:
17:11:36. 948 ModelValueTest1.0 [8386: 60b] {
AddressDic = {
Area = U6d77U6dc0U533a;
Provinces = U5317U4eac;
};
Age = 26;
Eventd = (
First,
Second,
Third
);
Name = Lisa;
}
17:11:36. 950 ModelValueTest1.0 [8386: 60b] name = Lisa
17:11:36. 951 ModelValueTest1.0 [8386: 60b] age = 26
17:11:36. 952 ModelValueTest1.0 [8386: 60b] addressDic = Beijing
17:11:36. 952 ModelValueTest1.0 [8386: 60b] eventd = (
First,
Second,
Third
)
The following are two core codes (compliant with a single function ):