[iOS base Control-4.2] dictionary to model

Source: Internet
Author: User

A. Disadvantages of using a dictionary to load data 1. User-specified key, error 2. Key is required for deposit and removal, easy to clutter B. Model (models in MVC) 1. Dictionary vs. Model: (1) Dictionary: Store data, value by key of string type (easy to write wrong, Write wrong key compiler will not error) (2) Model: Store data, custom attribute store data, actually similar to JavaBean, the essence is data Encapsulation 2. Implementation (1) Defining model classes
1 @interfaceApp:nsobject2 3 /**4 copy:nsstring5 Strong: General Objects6 Weak:ui Controls7 Assign: Basic data Type8  */9 Ten /** One name A  */ -@property (nonatomic, copy) NSString *name; -  the /** - icons -  */ -@property (nonatomic, copy) NSString *icon; + @end
(2) Use the model to read storage, read data storage:
1 #pragmaMark Gets the app list2-(Nsarray *) Apps {3     if(Nil = =_apps) {4         //1. Get the full path of the plist5NSString *path = [[NSBundle mainbundle] Pathforresource:@"app.plist"Oftype:nil];6        7         //2. Loading Data8Nsarray *dictarray =[Nsarray Arraywithcontentsoffile:path];9        Ten         //3. Convert all dictionaries inside the dictarray into a model and put them in a new array OneNsmutablearray *apparray =[Nsmutablearray array]; A          for(Nsdictionary *dictinchDictarray) { -             //3.1 Creating a Model object -APP *app =[[App alloc] init]; the             -             //3.2 Assigning all properties of a dictionary to a model -App.name =Dict[name_key]; -App.icon =Dict[icon_key]; +             -             //3.3 Add to app array + [Apparray Addobject:app]; A         } at         -_apps =Apparray; -     } -  -     return_apps; -}
Read:
1         Iconview.image = [UIImage ImageNamed:appData.icon]; 2         [Appview Addsubview:iconview]; 3        4         // 2. Set the app name 5         UILabel  *namelabel = [[UILabel alloc] init]; 6         Namelabel.text = Appdata.name;
C.model improves the analysis and storage of data in Viewcontrol, increases the coupling of data and classes, and the controller cares too much, which makes it difficult to modify the model after it has been modified. Solution: ==> So in model, parse the data 1. Implementation (1) custom constructed method declaration with data parameters:
1 /* * 2 Custom Construction Methods 3 initializing a Model object with a dictionary 4  */ 5 -(ID) initwithdictionary: (Nsdictionary *) dictionary;
Realize:
1 -(ID) initwithdictionary: (nsdictionary *) dictionary {2     if (self = [Super Init]) {3         self.name = Dictionary[name_key]; 4         Self.icon = Dictionary[icon_key]; 5     }6    7     return self ; 8 }
Use:
1             // 3.1 Creating a Model object 2             App *app = [[App Alloc] initwithdictionary:dict]; 3            4             // 3.2 Add to app array 5             [Apparray Addobject:app];
(2) Further provision of class methods
1 + (ID) appwithdictionary: (nsdictionary *) dictionary {2     //  Use self to represent the class name instead of the real class name, preventing subclass invocation Error 3     return  [[Self alloc] initwithdictionary:dictionary]; 4 }
D. Use "Instancetype" instead of "id" ID: Refers to all types, but when using inappropriate type pointers to mismatched objects, the compiler will not give an error, warning, but at run time it will likely error Instantcetype: The compiler can be based on the object that is currently pointing, Automatically checks if the variable type is correct, and then issues a warning

[iOS base Control-4.2] dictionary to 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.