IOS development-BaseModel, ios-basemodel

Source: Internet
Author: User

IOS development-BaseModel, ios-basemodel

During software development, various Data needs to be parsed, such as the most basic plist file, json Data downloaded from the network, XML webpage Data, and important Core Data.

Next I will share a method to quickly convert the dictionary in the json file into a model. although many people use third-party class libraries to parse json data, you still need to understand the implementation principle of converting the dictionary in the json file into the model.

The following is the BaseModel. h declaration file, which opens two methods to the outside world:

The method of Row 3 is to implement dictionary-to-model conversion in the json file;

The 10th line method prevents program exceptions caused by the failure to convert data of special types into models.

1 BaseModel. h 2 3 # import <Foundation/Foundation. h> 4 5 @ interface BaseModel: NSObject 6 7-(instancetype) initWithDictionary :( NSDictionary *) jsonDictionary; 8 9 // hand over the value in json to the attribute of the model (override this method can add special data in the json file to the model) special types such: NSNull data 10-(void) setAttributesWithDictionary :( NSDictionary *) jsonDict; 11 12 @ end

  

The following is the BaseModel. m implementation file.

 

1 BaseModel. m 2 3 # import "BaseModel. h "4 5 @ implementation BaseModel 6 7-(instancetype) initWithDictionary :( NSDictionary *) jsonDictionary 8 {9 self = [super init]; 10 11 if (self) {12 // hand over the value of the jsonDictionary dictionary to the value of the model's attribute value 13 [self setAttributesWithDictionary: jsonDictionary]; 14} 15 return self; 16} 17 18 // assign the value of the jsonDictionary dictionary to the model's attribute value 19-(void) setAttributesWithDictionary :( NSDictionary *) jsonDictionary20 {21 // get ing relationship 22 NSDictionary * modelDictionary = [self attributesModel: jsonDictionary]; 23 24 for (NSString * jsonKey in modelDictionary) {25 // obtain the attribute name 26 NSString * modelAttributesNmae = [modelDictionary objectForKey: jsonKey]; 27 28 // obtain the Value 29 id jsonValue = [jsonDictionary objectForKey: jsonKey]; 30 31 // setter method for obtaining attributes 32 SEL setterSEL = [self stringToSEL: modelAttributesNmae]; 33 34 // if the Value is NULL, the Value is assigned "" (nothing) 35 if ([jsonValue isKindOfClass: [NSNull class]) {36 jsonValue = @ ""; 37} 38 39 // Warning: when mselector may cause a leak because its selector is unknown (memory mselector may cause memory leakage because its selector is unknown) 40 if ([self respondsToSelector: setterSEL]) {41 [self when mselector: setterSEL withObject: jsonValue]; 42} 43 44} 45} 46 47 // map the attribute names of keys and models in the dictionary to 48-(NSDictionary *) attributesModel :( NSDictionary *) jsonDictionary49 {50 NSMutableDictionary * dict = [[NSMutableDictionary alloc] init]; 51 52 // The property name is the same as the jsonDictionary key 53 for (NSString * jsonKey in jsonDictionary) {54 [dict: jsonKey forKey: jsonKey]; 55} 56 return dict; 57} 58 59 // setter method for obtaining attributes 60-(SEL) stringToSEL :( NSString *) modelAttributes61 {62 // capture the 63 NSString * firstString = [modelAttributes substringToIndex: 1]; 64 65 // upper letter 66 firstString = [firstString uppercaseString]; 67 68 // capture the content of other attribute names except the first letter 69 NSString * endString = [modelAttributes substringFromIndex: 1]; 70 71 // The setter method name 72 NSString * selString = [NSString stringWithFormat: @ "set % @:", firstString, endString]; 73 74 // convert the string to method 75 SEL selector = NSSelectorFromString (selString); 76 77 return selector; 78} 79 80 @ end

 

Here, the BaseModel. m implementation file 41st will report a warning. The warning content is shown above.

So far, a class that converts the dictionary in the json file into a model has been implemented.

You can test the BaseModel usage by yourself. We will not demonstrate it here.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.