字典转模型的三种方法之四:总结

本文详细介绍了在iOS开发中如何利用模型、控制器与数据加载技术进行高效的数据处理与展示,包括如何创建模型类、实现数据转换、加载数据并将其展示在界面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模型

 1 //
 2 //  YSFoodTypeModule.h
 3 //  YSUiSplitViewController
 4 //
 5 //  Created by ys on 15/12/12.
 6 //  Copyright (c) 2015年 ys. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface YSFoodTypeModule : NSObject
12 
13 @property (nonatomic,copy)NSString *idstr;
14 
15 @property (nonatomic,copy)NSString *name;
16 //若使用MJExtension则不用写以下两个方法
17 -(instancetype)initWithDict:(NSDictionary *)dict;
18 
19 +(instancetype)foodTypeWithDict:(NSDictionary *)dict;
20 
21 @end

模型对应的.m文件

 1 //
 2 //  YSFoodTypeModule.m
 3 //  YSUiSplitViewController
 4 //
 5 //  Created by ys on 15/12/12.
 6 //  Copyright (c) 2015年 ys. All rights reserved.
 7 //
 8 
 9 #import "YSFoodTypeModule.h"
10 
11 @implementation YSFoodTypeModule
12 
13 +(instancetype)foodTypeWithDict:(NSDictionary *)dict
14 {
15     return [[self alloc]initWithDict:dict];
16 }
17 
18 -(instancetype)initWithDict:(NSDictionary *)dict
19 {
20     if (self = [super init]) {
21         
22         self.name = dict[@"name"];
23         self.idstr = dict[@"idstr"];
24 //        //or KVC,其缺点是字典键在模型中必须有对应属性,否则崩溃,而MJExtension则可以解决这个问题
25 //        [self setValuesForKeysWithDictionary:dict];
26     }
27     return self;
28 }
29 
30 @end

 

控制器代码

 1 //
 2 //  YSFoodTypesTableViewController.m
 3 //  YSUiSplitViewController
 4 //
 5 //  Created by ys on 15/12/12.
 6 //  Copyright (c) 2015年 ys. All rights reserved.
 7 //
 8 
 9 #import "YSFoodTypesTableViewController.h"
10 #import "YSFoodTypeModule.h"
11 #import "MJExtension.h"
12 
13 @interface YSFoodTypesTableViewController ()
14 
15 @property (nonatomic,strong)NSArray *foodTypes;
16 
17 @end
18 
19 
20 @implementation YSFoodTypesTableViewController
21 
22 -(NSArray *)foodTypes
23 {
24     if (_foodTypes == nil) {
25         //这里是字典转模型的几个方法
26         
27         //----0,调用模型内部里的字典转模型方法
28 //            NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil]];
29 //            NSMutableArray *arrayM = [NSMutableArray array];
30 //            for (NSDictionary *dict in dictArray) {
31 //                YSFoodTypeModule *foodT = [YSFoodTypeModule foodTypeWithDict:dict];
32 //                [arrayM addObject:foodT];
33 //            }
34 //            _foodTypes = arrayM;
35         
36         //----1,用MJExtension里的字典转模型方法,不用自己再写字典转模型方法.mj_objectWithKeyValues:dict
37 //        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil]];
38 //        NSMutableArray *arrayM = [NSMutableArray array];
39 //        for (NSDictionary *dict in dictArray) {
40 //            YSFoodTypeModule *foodT = [YSFoodTypeModule mj_objectWithKeyValues:dict];
41 //            [arrayM addObject:foodT];
42 //        }
43 //        _foodTypes = arrayM;
44         
45         //----2,用MJExtension里的字典数组转模型数组方法,不用自己再写字典转模型方法.mj_objectArrayWithKeyValuesArray:dictArray
46 //        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil]];
47 //        _foodTypes = [YSFoodTypeModule mj_objectArrayWithKeyValuesArray:dictArray];
48         
49         //----3,用MJExtension里的字典数组转模型数组方法,不用自己再写字典转模型方法.mj_objectArrayWithFile:filePath
50 //        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil];
51 //        _foodTypes = [YSFoodTypeModule mj_objectArrayWithFile:filePath];
52         
53         //----4,用MJExtension里的字典数组转模型数组方法,不用自己再写字典转模型方法.mj_objectArrayWithFilename
54         _foodTypes = [YSFoodTypeModule mj_objectArrayWithFilename:@"food_types.plist"];
55         
56     }
57     return _foodTypes;
58 }
59 
60 - (void)viewDidLoad {
61     [super viewDidLoad];
62 
63 }
64 
65 
66 #pragma mark - Table view data source
67 
68 
69 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
70     return self.foodTypes.count;
71 }
72 
73 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
74 {
75     NSString *ID = @"foodType";
76     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ID];
77     if (cell == nil) {
78         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
79     }
80     YSFoodTypeModule *foodType = self.foodTypes[indexPath.row];
81     cell.textLabel.text = foodType.name;
82     return cell;
83 }
84 
85 @end

 

转载于:https://www.cnblogs.com/yangshun-work/p/5041760.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值