A summary of the method of dictionary turn model

Source: Internet
Author: User

The first step: convert the elements of the dictionary into instance variables, in which the types are different;

The code is as follows:

1 //2 //HMQuestion.h3 //01-Super Guessing chart4 //5 //Created by Administrator on 15/10/7.6 //Copyright 2015 CCSU. All rights reserved.7 //8 9#import <Foundation/Foundation.h>Ten  One @interface Hmquestion:nsobject A@property (nonatomic,copy) NSString *answer;// Note the NSString type is copy  -@property (nonatomic,copy) NSString *icon; -@property (nonatomic,copy) NSString *title; the@property (nonatomic,strong) Nsarray *options;//nsarray type is strong  -  --(Instancetype) Initwithdict: (Nsdictionary *) dict; -+ (Instancetype) questionwithdict: (Nsdictionary *) dict; + /** Returns an array of all topics*/ -+ (Nsarray *) questions; +@end

Third step Write class method: (note logic)

Fourth Step dictionary to model: (note steps)

1 //2 //hmquestion.m3 //01-Super Guessing chart4 //5 //Created by Administrator on 15/10/7.6 //Copyright 2015 CCSU. All rights reserved.7 //8 9#import"HMQuestion.h"Ten  One @implementation hmquestion A-(Instancetype) Initwithdict: (Nsdictionary *) dict{ -Self =[Super init]; -     if(self) { the [self setvaluesforkeyswithdictionary:dict]; -     } -     returnSelf ; - } ++ (Instancetype) questionwithdict: (Nsdictionary *) dict{ -  +     return[[Self alloc]initwithdict:dict]; A } at  -  -+ (Nsarray *) questions{ -Nsarray *array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle]pathforresource:@"questions.plist"Oftype:nil]]; -Nsmutablearray *arraym =[Nsmutablearray array]; -      in      for(Nsdictionary *dicinchArray) { - [Arraym addobject:[self questionwithdict:dic]; to          +     } -     returnArraym; the}

Fifth step: Create an Instance object with reference to the file in the main file for lazy loading:

1 //2 //VIEWCONTROLLER.M3 //01-Super Guessing chart4 //5 //Created by Administrator on 15/10/6.6 //Copyright 2015 CCSU. All rights reserved.7 //8 9#import"ViewController.h"Ten#import"HMQuestion.h" One  A @interface Viewcontroller () -@property (Weak, nonatomic) Iboutlet UIButton *Iconbutton; -@property (nonatomic,strong) UIButton *cover; the@property (nonatomic,strong) Nsarray *questions; - @end -  - @implementation Viewcontroller +  --(Nsarray *) Questions + { A     if(_questions = =Nil) { at_questions =[hmquestion questions]; -     } -     return_questions; -  -}

Sixth step: Test success

 1 -(void  ) viewdidload  2  {  [ Super Viewdidload];  4  NSLog (@ " %@   " ,self.questions);  5   for (Hmquestion *obj in self.questions)    { 6  //  7  "  8 } 

Test optimization: Override the Description method (if the occurrence of Chinese may be garbled, so you need to introduce Nsarray+log.h and NSARRAY+LOG.M files, directly add it, without the header file reference):

1 -(NSString *) description2{3     return [NSString stringWithFormat:@ "  <%@:%p>{answer:%@,icon:%@,title:%@, options:%@}",
Self. class , self,self.answer,self.icon,self.title,self.options]; 4 5 6 }

The optimizations in Viewdidload are as follows:

 1 -(void  ) viewdidload  2  {  [ Super Viewdidload];  4   NSLog (@ "%@", self.questions);  5  for  (hmquestion *obj in   Self.questions) { 6  NSLog (
   
    @ " 
    %@  
    "  
     7   8  }
   

Additional: Nsarray+log.h and NSARRAY+LOG.M files:

1 //2 //nsarray+log.h3 //Application Management4 //5 //Created by Fan Liu on 14/8/14.6 //Copyright (c) 2014 itcast. All rights reserved.7 //8 9#import <Foundation/Foundation.h>Ten  One @interface Nsarray (Log) A  -@end

1 //2 //NSARRAY+LOG.M3 //Application Management4 //5 //Created by Fan Liu on 14/8/14.6 //Copyright (c) 2014 itcast. All rights reserved.7 //8 9#import"nsarray+log.h"Ten  One @implementation Nsarray (Log) A  --(NSString *) Descriptionwithlocale: (ID) locale - { thensmutablestring *STRM = [nsmutablestring stringwithstring:@"(\ n"]; -      -      for(ID objinchSelf ) { -[StrM AppendFormat:@"\t%@,\n", obj]; +     } -      +[StrM appendString:@") \ n"]; A      at     returnStrM; - } -  -@end

A summary of the method of dictionary turn model

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.