iOS????????Runtime??Model???
???????????? ???????[ 2015/7/30 13:48:46 ] ???????????????
?????????????????械??????
???????????械??????????Model?谐???????????????????????????????????????写??掳?????????getter??????????setter?????????Model????????????????????????Model?? ????????getter????????????????????斜?????????????谢????????????渭???????????????????????????
????1.????Key?????set????
???????????写?????????????????????????????????????????setter???????????????????????????????????????????写?????????set????????????SEL????????梅??????????????锟�?
1 #pragma mark -- ???????????????????????Setter????????????
2 - (SEL) creatSetterWithPropertyName: (NSString *) propertyName{
3
4 //1.???????写
5 propertyName = propertyName.capitalizedString;
6
7 //2.?????set?????
8 propertyName = [NSString stringWithFormat:@"set%@:"?? propertyName];
9
10 //3.????set????
11 return NSSelectorFromString(propertyName);
12 }
????2.????????????校?????????????????????????????梅????????????????????????setter?????????setter??????????Value??????????????????????????锟�?????????械??????????????????????慰?????????????????????????????????????????????????????????????????????????????????????key????????????????????????械?小????????????????????????????????????????????薪????
1 /************************************************************************
2 *????涓�????????????????
3 *?????????
4 *?????????????????????????key??????????????????????????????????Value
5 * ???????????????
6 ************************************************************************/
7
8 -(void) assginToPropertyWithDictionary: (NSDictionary *) data{
9
10 if (data == nil) {
11 return;
12 }
13
14 ///1.???????key
15 NSArray *dicKey = [data allKeys];
16
17 ///2.??????????key?? ????????????????setter????????????Value???setter????
18 ///???????????????
19 for (int i = 0; i < dicKey.count; i ++) {
20
21 ///2.1 ???getSetterSelWithAttibuteName ???????????????set????
22 SEL setSel = [self creatSetterWithPropertyName:dicKey[i]];
23
24 if ([self respondsToSelector:setSel]) {
25 ///2.2 ????????key?????value
26 NSString *value = [NSString stringWithFormat:@"%@"?? data[dicKey[i]]];
27
28 ///2.3 ??????setter???????????????????
29 [self performSelectorOnMainThread:setSel
30 withObject:value
31 waitUntilDone:[NSThread isMainThread]];
32 }
33
34 }
35
36 }
????3.????????????写???????????????????????????????????????????????????????????????????????????????????????????????????
??????1???????????????????????????????????????????????梅???????????????????????械?key??????????????????????????????????????????????????????????????????????-(void) assginToPropertyWithDictionary: (NSDictionary *) data????????????????锟�?
????1 - (instancetype)initWithDictionary: (NSDictionary *) data{
????2 {
????3 self = [super init];
????4 if (self) {
????5 [self assginToPropertyWithDictionary:data];
????6 }
????7 return self;
????8 }
????9 }
??????2?????娼�???????????????????????????????????????????????????????????????????????????????????????????????????????????????????路???????????????????????
????1 + (instancetype)modelWithDictionary: (NSDictionary *) data{
????2
????3 return [[self alloc] initWithDictionary:data];
????4
????5 }
?????濉�????????????
??????????????????????????鈥�?????????伞????????????????????????????????????????main?????薪??械????????写?????????????????????????????????????????????校?????????????????????????????????????械??????????????????????????????????梅????????????
????1 BeautifulGirlModel *beautifulGirl = [BeautifulGirlModel modelWithDictionary:data];
????2
????3 NSLog(@"%@"?? beautifulGirl.girl0);
???????薪?????锟�?
?????????????????????????????????????????????????????????????????????????????????????????????锟�?
????1 BeautifulGirlModel *beautifulGirl1 = [[BeautifulGirlModel alloc] init];
????2 beautifulGirl1.girl0 = data[@"girl0"];
????3 beautifulGirl1.girl1 = data[@"girl1"];
????4 beautifulGirl1.girl2 = data[@"girl2"];
????5 beautifulGirl1.girl3 = data[@"girl3"];
????6 beautifulGirl1.girl4 = data[@"girl4"];
????7 beautifulGirl1.girl5 = data[@"girl5"];
????8 beautifulGirl1.girl6 = data[@"girl6"];
????9 beautifulGirl1.girl7 = data[@"girl7"];
??????

???路???
??????????????????
2023/3/23 14:23:39???写?貌??????????
2023/3/22 16:17:39????????????????????些??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???路???????路
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11