Teach you to use runtime to write a simple dictionary-to-model tool class, runtime tool class
1. automatically generate the attribute category
Model attributes usually need to correspond to the keys in the dictionary one by one. Is the data obtained from the server too complex? Too much data? Is it too cumbersome to enter properties one by one after writing plist files? Then I can try to write a category to automatically print all attributes.
• Requirement: whether a dictionary can automatically generate corresponding attributes.
• Solution: provides a category to generate corresponding attribute strings Based on the dictionary.
1 # import <Foundation/Foundation. h> 2 3 @ interface NSDictionary (PropertyCode) 4 5 // generate attribute code 6-(void) createPropetyCode; 7 @ end 8 9 @ implementation NSDictionary (PropertyCode) 10 // Private API: actually exists, but apple does not expose it and you are not allowed to use 11 // isKindOfClass: determine whether the current class or subclass is 12 // automatically generate the attribute code 13-(void) createPropetyCode14 {15 NSMutableString * codes = [NSMutableString string]; 16 // traverse the dictionary 17 [self enumerateKeysAndObjectsUsingBlock: ^ (id _ Nonnull key, id _ Nonnull value, BOOL * _ Nonnull stop) {18 NSString * code = nil; 19 20 if ([value isKindOfClass: [NSString class]) {// Note: NSString * I like to use strong. To use copy, you can modify the string 21 code = [NSString stringWithFormat: @ "@ property (nonatomic, strong) NSString * % @;", key]; 22} else if ([value isKindOfClass: NSClassFromString (@ "_ NSCFBoolean")]) {23 code = [NSString stringWithFormat: @ "@ property (nonatomic, assign) BOOL % @;", key]; 24} else if ([value isKindOfClass: [NSNumber class]) {25 code = [NSString stringWithFormat: @ "@ property (nonatomic, assign) NSInteger % @;", key]; 26} else if ([value isKindOfClass: [NSArray class]) {27 code = [NSString stringWithFormat: @ "@ property (nonatomic, strong) NSArray * % @;", key]; 28} else if ([value isKindOfClass: [NSDictionary class]) {29 code = [NSString stringWithFormat: @ "@ property (nonatomic, strong) NSDictionary * % @; ", key]; 30} 31 32 // concatenated string 33 [codes appendFormat: @" \ n % @ \ n ", code]; 34 35}]; 36 37 NSLog (@ "% @", codes); 38} 39 @ endClassification NSDictionary + PropertyCode
External Use:
1 # import "ViewController. h "2 # import" Status. h "3 # import" NSDictionary + PropertyCode. h "4/* 5 plist: 6 Dictionary 7 dictionary to Model 8 */9 10 @ interface ViewController () 11 12 @ end13 14 @ implementation ViewController15 16-(void) viewDidLoad {17 [super viewDidLoad]; 18 19 // parse Plist20 // obtain the full file path 21 NSString * fileName = [[NSBundle mainBundle] pathForResource: @ "status. plist "ofType: nil]; 22 // obtain the dictionary 23 NSDictionary * dict = [NSDictionary dictionaryWithContentsOfFile: fileName]; 24 25 // design model-define attribute 26 // automatically generate attribute code 27 [dict createPropetyCode]; 28} 29 @ endViewController
Status. plist
Console print information:
10:43:34. 959 Runtime (automatically generated attribute) [38837: 1398902] @ property (nonatomic, assign) BOOL aaa; @ property (nonatomic, assign) NSInteger reposts_count; @ property (nonatomic, copy) NSString * source; @ property (nonatomic, strong) NSArray * pic_urls; @ property (nonatomic, copy) NSString * created_at; @ property (nonatomic, assign) NSInteger attitudes_count; @ property (nonatomic, copy) NSString * idstr; @ property (nonatomic, copy) NSString * text; @ property (nonatomic, assign) NSInteger comments_count; @ property (nonatomic, strong) NSDictionary * user;Console printing information II. dictionary-to-model KVC implementation
KVC must ensure that the attribute names in the model must match the keys in the dictionary one by one.
Requirement: A lot of data is usually provided in the background during development, but not every data is useful. Do you need to save the useless data to the model?