标签:
第一步:将字典的元素转为实例变量,其中注意的是类型的不同;
代码如下:
1 // 2 // HMQuestion.h 3 // 01-超级猜图 4 // 5 // Created by Administrator on 15/10/7. 6 // Copyright 2015年 ccsu. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface HMQuestion : NSObject 12 @property (nonatomic,copy) NSString *answer; //注意NSString类型的是copy 13 @property (nonatomic,copy) NSString *icon; 14 @property (nonatomic,copy) NSString *title; 15 @property (nonatomic,strong) NSArray *options;//NSArray类型的是strong 16 17 -(instancetype)initWithDict:(NSDictionary *)dict; 18 +(instancetype)questionWithDict:(NSDictionary *)dict; 19 /**返回所有题目数组*/ 20 +(NSArray *)questions; 21 @end
第三步写类方法:(注意逻辑)
第四步字典转模型:(注意步骤)
1 // 2 // HMQuestion.m 3 // 01-超级猜图 4 // 5 // Created by Administrator on 15/10/7. 6 // Copyright 2015年 ccsu. All rights reserved. 7 // 8 9 #import "HMQuestion.h" 10 11 @implementation HMQuestion 12 -(instancetype)initWithDict:(NSDictionary *)dict{ 13 self = [super init]; 14 if (self) { 15 [self setValuesForKeysWithDictionary:dict]; 16 } 17 return self; 18 } 19 +(instancetype)questionWithDict:(NSDictionary *)dict{ 20 21 return [[self alloc]initWithDict:dict]; 22 } 23 24 25 +(NSArray *)questions{ 26 NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"questions.plist" ofType:nil]]; 27 NSMutableArray *arrayM = [NSMutableArray array]; 28 29 for (NSDictionary *dic in array ) { 30 [arrayM addObject:[self questionWithDict:dic]]; 31 32 } 33 return arrayM; 34 }
第五步:在主文件中引用文件创建实例对象,进行懒加载:
1 // 2 // ViewController.m 3 // 01-超级猜图 4 // 5 // Created by Administrator on 15/10/6. 6 // Copyright 2015年 ccsu. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "HMQuestion.h" 11 12 @interface ViewController () 13 @property (weak, nonatomic) IBOutlet UIButton *iconButton; 14 @property(nonatomic,strong)UIButton *cover; 15 @property(nonatomic,strong)NSArray *questions; 16 @end 17 18 @implementation ViewController 19 20 -(NSArray *)questions 21 { 22 if (_questions == nil) { 23 _questions = [HMQuestion questions]; 24 } 25 return _questions; 26 27 }
第六步 :测试是否成功
1 -(void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 NSLog(@"%@",self.questions); 5 // for (HMQuestion *obj in self.questions) { 6 // NSLog(@"%@",obj); 7 // } 8 }
测试优化:重写description方法(如果出现中文可能会出现乱码,所以需要引入NSArray+Log.h和NSArray+Log.m文件,直接添加就可以,不用头文件引用):
1 -(NSString *)description 2 { 3 return [NSString stringWithFormat:@" <%@: %p>{answer:%@,icon:%@,title:%@, options:%@}",
self.class,self,self.answer,self.icon,self.title,self.options]; 4 5 6 }
在viewDidLoad中优化如下:
1 -(void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 //NSLog(@"%@",self.questions); 5 for (HMQuestion *obj in self.questions) { 6 NSLog(@"%@",obj); 7 } 8 }
附加:NSArray+Log.h和NSArray+Log.m文件:
1 // 2 // NSArray+Log.h 3 // 应用程序管理 4 // 5 // Created by 刘凡 on 14/8/14. 6 // Copyright (c) 2014年 itcast. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface NSArray (Log) 12 13 @end
1 // 2 // NSArray+Log.m 3 // 应用程序管理 4 // 5 // Created by 刘凡 on 14/8/14. 6 // Copyright (c) 2014年 itcast. All rights reserved. 7 // 8 9 #import "NSArray+Log.h" 10 11 @implementation NSArray (Log) 12 13 - (NSString *)descriptionWithLocale:(id)locale 14 { 15 NSMutableString *strM = [NSMutableString stringWithString:@"(\n"]; 16 17 for (id obj in self) { 18 [strM appendFormat:@"\t%@,\n", obj]; 19 } 20 21 [strM appendString:@")\n"]; 22 23 return strM; 24 } 25 26 @end
标签:
原文地址:http://www.cnblogs.com/Simplelee/p/4859853.html