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