·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> [iOS基础控件-4.2]APP列表字典转模型Model

[iOS基础控件-4.2]APP列表字典转模型Model

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23
A.使用字典加载数据的缺点 1.用户自行指定key,容易出错 2.存入、取出都需要key,容易混乱   B.模型 (MVC中的model) 1.字典与模型对比: (1)字典:存储数据,通过字符串类型的key取值(容易写错,写错了key编译器不会报错) (2)模型:存储数据,自定义属性存储数据,其实就类似javaBean,本质是数据封装   2.实现 (1)定义模型类
 1 @interface App : NSObject
 2 
 3 /**
 4  copy : NSString
 5  strong: 一般对象
 6  weak: UI控件
 7  assign: 基本数据类型
 8  */
 9 
10 /**
11  名称
12  */
13 @PRoperty(nonatomic, copy) NSString *name;
14 
15 /**
16  图标
17  */
18 @property(nonatomic, copy) NSString *icon;
19 @end
  (2)使用模型读取存储、读取数据 存储:
 1 #pragma mark 取得应用列表
 2 - (NSArray *) apps {
 3     if (nil == _apps) {
 4         // 1.获得plist的全路径
 5         NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
 6        
 7         // 2.加载数据
 8         NSArray *dictArray  = [NSArray arrayWithContentsOfFile:path];
 9        
10         // 3.将dictArray里面的所有字典转成模型,放到新数组中
11         NSMutableArray *appArray = [NSMutableArray array];
12         for (NSDictionary *dict in dictArray) {
13             // 3.1创建模型对象
14             App *app = [[App alloc] init];
15            
16             // 3.2 将字典的所有属性赋值给模型
17             app.name = dict[NAME_KEY];
18             app.icon = dict[ICON_KEY];
19            
20             // 3.3 添加到app数组中
21             [appArray addObject:app];
22         }
23        
24         _apps = appArray;
25     }
26 
27     return _apps;
28 }
  读取:
1         iconView.image = [UIImage imageNamed:appData.icon];
2         [appView addSubview:iconView];
3        
4         // 2.设置APP名字
5         UILabel  *nameLabel = [[UILabel alloc] init];
6         nameLabel.text = appData.name;
  C.Model改进      在ViewControl中解析、存储数据, 增加了数据、类的耦合性,控制器关心太多,在Model被修改之后难以进行自我修改.      解决 : ==> 所以在Model中解析数据 1.实现 (1)自定义带数据参数的构造方法 声明:
1 /**
2  自定义构造方法
3  通过字典来初始化模型对象
4  */
5 - (id) initWithDictionary:(NSDictionary *) dictionary;
  实现:
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 }
  使用:
1             // 3.1创建模型对象
2             App *app = [[App alloc] initWithDictionary:dict];
3            
4             // 3.2 添加到app数组中
5             [appArray addObject:app];
  (2)进一步提供类方法
1 + (id) appWithDictionary:(NSDictionary *) dictionary {
2     // 使用self代表类名代替真实类名,防止子类调用出错
3     return [[self alloc] initWithDictionary:dictionary];
4 }
  D.使用”instancetype”代替”id" id: 代指所有类型,但是使用不恰当的类型指针指向不匹配的对象时,编译器不会报错、警告,但在运行时很可能会出错 Image   instantcetype: 1.能指向任何类型 2.编译器能够根据当前指向的对象,自动检查变量类型是否正确,进而发出警告 3.只能作为返回值,不能用作参数

Image(4)