首页 > 代码库 > ios开发 Reflection(三) 利用反射自动绑值

ios开发 Reflection(三) 利用反射自动绑值

反射的具体应用,自动绑值

 

获取属性列表

 1 - (NSArray*)propertyKeys 2 { 3     unsigned int outCount, i; 4     objc_property_t *properties = class_copyPropertyList([self class], &outCount); 5     NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:outCount]; 6     for (i = 0; i < outCount; i++) { 7         objc_property_t property = properties[i]; 8         NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; 9         [keys addObject:propertyName];10     }11     free(properties);12     return keys;13 }

 

字典与属性列表形成映射

 1 - (BOOL)reflectDataFromOtherObject:(NSObject*)dataSource 2 { 3     BOOL ret = NO; 4     for (NSString *key in [self propertyKeys]) { 5         if ([dataSource isKindOfClass:[NSDictionary class]]) { 6             ret = ([dataSource valueForKey:key]==nil)?NO:YES; 7         } 8         else 9         {10             ret = [dataSource respondsToSelector:NSSelectorFromString(key)];11         }12         if (ret) {13             id propertyValue =http://www.mamicode.com/ [dataSource valueForKey:key];14             //该值不为NSNULL,并且也不为nil15             if (![propertyValue isKindOfClass:[NSNull class]] && propertyValue!=nil) {16                 [self setValue:propertyValue forKey:key];17             }18         }19     }20     return ret;21 }

 

下面是BookModel类的自动绑值的实现

 1 #import <Foundation/Foundation.h> 2 @interface BookModel : NSObject 3 { 4     NSString* _iconUrl; 5     NSString* _bookName; 6     NSString* _publisher; 7     NSString* _price; 8     NSString* _autherName; 9     NSString* _pubdate;10     NSString* _translator;11     NSString* _introUrl;12     NSInteger _numRatings;13     CGFloat _rating;14 }15 @property(retain, nonatomic)NSString* iconUrl;16 @property(retain, nonatomic)NSString* pubdate;17 @property(retain, nonatomic)NSString* introUrl;18 @property(retain, nonatomic)NSString* autherName;19 @property(retain, nonatomic)NSString* translator;;20 @property(retain, nonatomic)NSString* bookName;21 @property(retain, nonatomic)NSString* publisher;22 @property(retain, nonatomic)NSString* price;23 @property(assign, nonatomic)NSInteger numRatings;24 @property(assign, nonatomic)CGFloat rating;25 26 - (NSArray*)propertyKeys;27 28 - (BOOL)reflectDataFromOtherObject:(NSObject*)dataSource;29 30 @end
 1 #import "BookModel.h" 2 #import <objc/runtime.h> 3  4 @implementation BookModel 5 @synthesize bookName = _bookName; 6 @synthesize iconUrl = _iconUrl; 7 @synthesize publisher = _publisher; 8 @synthesize price = _price; 9 @synthesize numRatings = _numRatings;10 @synthesize rating = _rating;11 @synthesize autherName = _autherName;12 @synthesize pubdate = _pubdate;13 @synthesize translator = _translator;14 @synthesize introUrl = _introUrl;15 16 - (id)init17 {18     self = [super init];19     if (self) {20         // Initialization code here.21     }22     23     return self;24 }25 26 - (NSArray*)propertyKeys27 {28     unsigned int outCount, i;29     objc_property_t *properties = class_copyPropertyList([self class], &outCount);30     NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:outCount];31     for (i = 0; i < outCount; i++) {32         objc_property_t property = properties[i];33         NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];34         [keys addObject:propertyName];35     }36     free(properties);37     return keys;38 }39 40 - (BOOL)reflectDataFromOtherObject:(NSObject*)dataSource41 {42     BOOL ret = NO;43     for (NSString *key in [self propertyKeys]) {44         if ([dataSource isKindOfClass:[NSDictionary class]]) {45             ret = ([dataSource valueForKey:key]==nil)?NO:YES;46         }47         else48         {49             ret = [dataSource respondsToSelector:NSSelectorFromString(key)];50         }51         if (ret) {52             id propertyValue =http://www.mamicode.com/ [dataSource valueForKey:key];53             //该值不为NSNULL,并且也不为nil54             if (![propertyValue isKindOfClass:[NSNull class]] && propertyValue!=nil) {55                 [self setValue:propertyValue forKey:key];56             }57         }58     }59     return ret;60 }61 62 63 @end
 1 #import "ViewController.h" 2 #import "BookModel.h" 3  4  5 @interface ViewController () 6  7 @end 8  9 @implementation ViewController10 11 - (void)viewDidLoad12 {13     [super viewDidLoad];14     // Do any additional setup after loading the view, typically from a nib.15     NSMutableDictionary *curDic=[[NSMutableDictionary alloc]init];16     [curDic setValue:@"iconUrlffwf" forKey:@"iconUrl"];17     [curDic setValue:@"pubdatefewfewf" forKey:@"pubdate"];18     [curDic setValue:@"introUrlfefewf" forKey:@"introUrl"];19     [curDic setValue:@"autherName" forKey:@"autherNamefwfwfw"];20     [curDic setValue:@"translatorfwfewf" forKey:@"translator"];21     [curDic setValue:@"bookNamefvvvv" forKey:@"bookName"];22     [curDic setValue:@"publisherfwfewf" forKey:@"publisher"];23     [curDic setValue:@"pricefwfwf" forKey:@"price"];24     [curDic setValue:@"numRatingsssss" forKey:@"numRatings"];25     [curDic setValue:@"ratinggggggg" forKey:@"rating"];26 27     BookModel *bModel=[[BookModel alloc]init];28     [bModel reflectDataFromOtherObject:curDic];29     NSLog(@"bModel:%@",bModel.translator);30     31 }32 33 - (void)didReceiveMemoryWarning34 {35     [super didReceiveMemoryWarning];36     // Dispose of any resources that can be recreated.37 }38 39 @end

 

ios开发 Reflection(三) 利用反射自动绑值