<span id="Label3"></p><p><p>Use the model to store the contents of the dictionary for easy access</p></p><p><p></p></p><p><p>first, Create Class Mjhero</p></p><p><p>1. Declaring attributes in header files, declaring object methods, class methods</p></p><p><p>#import <Foundation/Foundation.h><br><br>@interface Mjhero:nsobject<br>@property (nonatomic, copy) nsstring *name;<br>@property (nonatomic, copy) nsstring *icon;<br>@property (nonatomic, copy) nsstring *intro;<br><br>+ (instancetype) herowithdict: (nsdictionary *) dict;<br>-(instancetype) initwithdict: (nsdictionary *) dict;<br>@end<br><br></p></p><p><p>2, in The. m implementation method</p></p><p><p>#import "MJHero.h"<br><br>@implementation Mjhero<br>+ (instancetype) herowithdict: (nsdictionary *) dict<br>{<br>return [[self alloc] initwithdict:dict];<br>}<br><br>-(instancetype) initwithdict: (nsdictionary *) dict<br>{<br>If (self = [super init]) {<br>[self setvaluesforkeyswithdictionary:dict];<br>}<br>Return self;<br>}<br>@end<br><br></p></p><p><p>Ii. adding arrays and get methods in VIEWCONTROLLER.M</p></p><p><p>#import "MJViewController.h"<br>#import "MJHero.h"</p></p><p><p>@property (nonatomic, Strong) Nsarray *heros;</p></p><p>-(nsarray *) heros<br>{<br>if (_heros = = Nil) {<br>Initialization<br>1. Get the full path of the plist<br>NSString *path = [[nsbundle mainbundle] pathforresource:@ "heros.plist" oftype:nil];<br><br>2. Loading an array<br>Nsarray *dictarray = [nsarray arraywithcontentsoffile:path];<br><br>3. Convert all dictionaries inside the Dictarray into model objects and put them in a new array<br>Nsmutablearray *heroarray = [nsmutablearray array];<br>For (nsdictionary *dict in Dictarray) {<br>3.1. Create a Model object<br>Mjhero *hero = [mjhero herowithdict:dict];<br><br>3.2. adding model objects to the array<br>[heroarray addobject:hero];<br>}<br><br>4. Assigning values<br>_heros = heroarray;<br>}<br>Return _heros;<br>}<br><br></p> <p><p> IOS dictionary to model </p> </p></span>
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