Using runtime to parse Modelby Wusheying
+ (Instancetype) Modelwithdictionary: (nsdictionary*) data{
return [[ SelfAlloc]initwithdictionary:d ATA];
}
- (Instancetype) Initwithdictionary: (nsdictionary*) data{
{
Self = [SuperInit];
if ( Self) {
[ Selfassgintopropertywithdictionary:d ATA];
}
return Self;
}
}
-(void) Assgintopropertywithdictionary: (nsdictionary*) data{
if (data = = Nil) {
return;
}
Nsarray *dickey = [DataAllKeys];
for (inti = 0; i < DicKey.Count; i + +) {
SEL SetSel = [ SelfCreatsetterwithpropertyname:d Ickey[i]];
if ([ SelfRespondstoselector: SetSel]) {
NSString*value = [NSStringstringWithFormat:@"%@", Data[dickey[i]];
[ SelfPerformselectoronmainthread: SetSel
Withobject: Value
Waituntildone:[NsthreadIsmainthread]];
}
}
}
- (SEL) Creatsetterwithpropertyname: (NSString*) propertyname{
PropertyName = PropertyName.capitalizedstring;
propertyname = [NSStringstringWithFormat:@ "set%@:", PropertyName];
return nsselectorfromstring(PropertyName);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Using Runtime to parse model