Dictionary to model object

Source: Internet
Author: User

Here the main point is to say nsdictionary to the core code of the model object, and need to pay attention to points.

/***
* From processing the data in the plist and returning an array of model objects
*
* @return Nsarray *apps;
*/
-(Nsarray *) apps{
if (_apps==nil) {
The whole path of the past plist
NSString *path=[[nsbundle mainbundle]pathforresource:@ "App.plist" oftype:nil];
Loading arrays
Nsarray *dicarray=[nsarray Arraywithcontentsoffile:path];
Converts all dictionaries inside the Dicarray into model objects and puts them in a new array.
Nsmutablearray *apparray=[nsmutablearray Array];
For (Nsdictionary *dict in Dicarray) {
To create a model object
//initwithdictCustom Object Methods
MYAPP *app=[[myapp alloc] initwithdict:dict];

/***[nsstring stringwithformat:<# (NSString *), ...#>];
[[NSString Alloc] initwithformat:<# (NSString *), ...#>];

[Nsarray arraywithcontentsoffile:<# (NSString *) #>]
[[Nsarray alloc] initwithcontentsoffile:<# (NSString *) #>;**/
/** through here we need to extract a appwith
MYAPP *app=[myapp Appwithdict:dict];
Add to object to array
[Apparray Addobject:app];
}
Assign value
_apps=dicarray;

}
return _apps;
}





#import <Foundation/Foundation.h>/** * Copy:nsstring Strong: General Object Weak:ui Control assign: Basic data type*/ @interface MyApp: Nsobject/** * icon */< Span style= "color: #000000;" > @property (nonatomic,copy) nsstring *icon;* * name */ @property ( nonatomic,copy) nsstring *name; /** * initializes the model object through a dictionary * * @param dic Dictionary Object * * @return already initialized model object *//*instancetype is to make methods of those non-associative return types return the type of the class in which they are located! The benefit can determine the type of object, can help the compiler to better locate the code for us to write problems
Instanchetype and ID comparison 1, the same point can be used as the return type of the method 2, different points ①instancetype can return and the method is the same class type of object, ID can only return an object of unknown type; ②instancetype can only be returned as a return value. Not as a parameter as the ID, such as the following:*/-(Instancetype) initwithdict: (Nsdictionary *) dict;+ (instancetype) Appwithdict: ( Nsdictionary *) dict; @end   

Dictionary to model object

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.