Convert a dictionary into a model object in IOS and a dictionary model in ios.

Source: Internet
Author: User

Convert a dictionary into a model object in IOS and a dictionary model in ios.

As a beginner in IOS development, I learned how to convert plist data dictionary into an array of data objects. It is a bit like the process of parsing xml data in C.

The xml data of apps. plist is like this.

 

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE plist PUBLIC "-// Apple // dtd plist 1.0 // EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version = "1.0"> <array> <dict> <key> name </key> <string> daily cool run </string> <key> icon </key> <string> icon_00 </string> </dict> <key> name </key> <string> defending radish 2 </string> <key> icon </key> <string> icon_10 </string> </dict> <key> name </key> <string> fake dad </string> <key> icon </key> <string> icon_11 </string> </dict> </array> </ plist>


From processing data in plist and returning an array of Model Objects
/*** Process data in plist and return the array of Model Objects ** @ return NSArray * apps; */-(NSArray *) apps {if (_ apps = nil) {// the full path NSString * path of plist in the past = [[NSBundle mainBundle] pathForResource: @ "app. plist "ofType: nil]; // load the array NSArray * dicArray = [NSArray arrayWithContentsOfFile: path]; // convert all dictionaries in dicArray into model objects and put them in the new array. NSMutableArray * appArray = [NSMutableArray array]; for (NSDictionary * dict in dicArray) {// create model object/* MyApp * app = [[MyApp alloc] initWithDict: dict]; [NSString stringWithFormat: <# (NSString *),... #>]; [[NSString alloc] initWithFormat: <# (NSString *),... #>]; [NSArray arrayWithContentsOfFile: <# (NSString *) #>] [[NSArray alloc] initWithContentsOfFile: <# (NSString *) #>; here we need to extract an appWith naming convention question */MyApp * app = [MyApp appWithDict: dict]; // Add it to the array [appArray addObject: app];} // value _ apps = dicArray;} return _ apps ;}

 

 

The custom MyApp class corresponds to the dictionary one by one.

# Import <Foundation/Foundation. h>/*** copy: NSString strong: general object weak: UI control assign: basic data type */@ interface MyApp: NSObject/*** icon */@ property (nonatomic, copy) NSString * icon;/*** name */@ property (nonatomic, copy) NSString * name; /*** initialize the model object through the dictionary ** @ param dic dictionary object ** @ return the initialized model object * // * instancetype, is the type of the class that the non-correlated return type returns! The benefits of determining the object type can help the compiler better locate code writing problems for us.
Comparison of instanchetype and id 1. similarities can be used as return types of Methods 2. Differences ① instancetype can return objects of the same type as the class where the method is located, and IDS can only return objects of unknown types; ② instancetype can only be used as the return value and cannot be used as the parameter as the id. For example, the following method is used: */-(instancetype) initWithDict :( NSDictionary *) dict; + (instancetype) appWithDict :( n *) dict; @ end

 

 

@ Implementation MyApp-(instancetype) initWithDict :( NSDictionary *) dict {if (self = [super init]) {self. name = dict [@ "name"]; self. icon = dict [@ "icon"];} return self;} + (instancetype) appWithDict :( NSDictionary *) dict {// Why is self used, who calls the self method will point to WHO !! Return [[self alloc] initWithDict: dict] ;}@ end

 

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.