关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结

一:Plist读取

  1. /******************************************************************************/
  2. 一:简单plist读取
  3.  
  4. :定义一个数组用来保存读取出来的plist数据
  5. @property (nonatomic, strong) NSArray *shops;
  6.  
  7. :使用懒加载的方式加载plist文件,并且放到数组中
  8. // 懒加载
  9. // 1.第一次用到时再去加载
  10. // 2.只会加载一次
  11. - (NSArray *)shops
  12. {
  13. if (_shops == nil) {
  14. // 获得plist文件的全路径
  15. NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
  16.  
  17. // 从plist文件中加载一个数组对象
  18. _shops = [NSArray arrayWithContentsOfFile:file];
  19. }
  20. return _shops;
  21. }
  22.  
  23. :使用数组中的数据
  24. // 设置数据
  25. NSDictionary *shop = self.shops[index];
  26. iconView.image = [UIImage imageNamed:shop[@"icon"]];
  27. nameLabel.text = shop[@"name"];
  28.  
  29. /******************************************************************************/

二:字典转模型

  1. 二:字典转模型
  2.  
  3. :创建一个model类并且在里面创建对应的模型属性
  4. /** 名字 */
  5. @property (nonatomic, strong) NSString *name;
  6. /** 图标 */
  7. @property (nonatomic, strong) NSString *icon;
  8.  
  9. :定义一个数组用来保存读取出来的plist数据
  10. @property (nonatomic, strong) NSMutableArray *shops;
  11.  
  12. :使用懒加载的方式加载plist文件,并且放到模型中
  13.  
  14. // 懒加载
  15. // 1.第一次用到时再去加载
  16. // 2.只会加载一次
  17. - (NSMutableArray *)shops
  18. {
  19. if (_shops == nil) {
  20. // 创建"模型数组"
  21. _shops = [NSMutableArray array];
  22.  
  23. // 获得plist文件的全路径
  24. NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
  25.  
  26. // 从plist文件中加载一个数组对象(这个数组中存放的都是NSDictionary对象)
  27. NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
  28.  
  29. // 将 “字典数组” 转换为 “模型数据”
  30. for (NSDictionary *dict in dictArray) { // 遍历每一个字典
  31. // 将 “字典” 转换为 “模型”
  32. Shop *shop = [[Shop alloc] init];
  33. shop.name = dict[@"name"];
  34. shop.icon = dict[@"icon"];
  35.  
  36. // 将 “模型” 添加到 “模型数组中”
  37. [_shops addObject:shop];
  38. }
  39. }
  40. return _shops;
  41. }
  42.  
  43. :使用模型中的数据
  44. // 设置数据
  45. Shop *shop = self.shops[index];
  46. iconView.image = [UIImage imageNamed:shop.icon];
  47. nameLabel.text = shop.name;
  48.  
  49. /******************************************************************************/

三:字典转模型疯转

  1. 二:字典转模型封装
  2.  
  3. :创建一个model类并且在里面创建对应的模型属性,定义两个模型方法
  4. /** 名字 */
  5. @property (nonatomic, copy) NSString *name;
  6. /** 图标 */
  7. @property (nonatomic, copy) NSString *icon;
  8.  
  9. /** 通过一个字典来初始化模型对象 */
  10. - (instancetype)initWithDict:(NSDictionary *)dict;
  11.  
  12. /** 通过一个字典来创建模型对象 */
  13. + (instancetype)shopWithDict:(NSDictionary *)dict;
  14.  
  15. :模型方法的实现
  16. - (instancetype)initWithDict:(NSDictionary *)dict
  17. {
  18. if (self = [super init]) {
  19. self.name = dict[@"name"];
  20. self.icon = dict[@"icon"];
  21. }
  22. return self;
  23. }
  24.  
  25. + (instancetype)shopWithDict:(NSDictionary *)dict
  26. {
  27. // 这里要用self
  28. return [[self alloc] initWithDict:dict];
  29. }
  30.  
  31. :定义一个数组用来保存读取出来的plist数据
  32. @property (nonatomic, strong) NSMutableArray *shops;
  33.  
  34. :使用懒加载的方式加载plist文件,并且放到模型中
  35. // 懒加载
  36. // 1.第一次用到时再去加载
  37. // 2.只会加载一次
  38. - (NSMutableArray *)shops
  39. {
  40. if (_shops == nil) {
  41. // 创建"模型数组"
  42. _shops = [NSMutableArray array];
  43.  
  44. // 获得plist文件的全路径
  45. NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
  46.  
  47. // 从plist文件中加载一个数组对象(这个数组中存放的都是NSDictionary对象)
  48. NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
  49.  
  50. // 将 “字典数组” 转换为 “模型数据”
  51. for (NSDictionary *dict in dictArray) { // 遍历每一个字典
  52. // 将 “字典” 转换为 “模型”
  53. XMGShop *shop = [XMGShop shopWithDict:dict];
  54.  
  55. // 将 “模型” 添加到 “模型数组中”
  56. [_shops addObject:shop];
  57. }
  58. }
  59. return _shops;
  60. }
  61.  
  62. :使用模型中的数据
  63. // 设置数据
  64. XMGShop *shop = self.shops[index];
  65. iconView.image = [UIImage imageNamed:shop.icon];
  66. nameLabel.text = shop.name;
  67.  
  68. /******************************************************************************/

四:自定义View

  1. 四:自定义View
  2.  
  3. :创建一个model类并且在里面创建对应的模型属性,定义两个模型方法
  4. /** 名字 */
  5. @property (nonatomic, copy) NSString *name;
  6. /** 图标 */
  7. @property (nonatomic, copy) NSString *icon;
  8.  
  9. /** 通过一个字典来初始化模型对象 */
  10. - (instancetype)initWithDict:(NSDictionary *)dict;
  11.  
  12. /** 通过一个字典来创建模型对象 */
  13. + (instancetype)shopWithDict:(NSDictionary *)dict;
  14.  
  15. :模型方法的实现
  16. - (instancetype)initWithDict:(NSDictionary *)dict
  17. {
  18. if (self = [super init]) {
  19. self.name = dict[@"name"];
  20. self.icon = dict[@"icon"];
  21. }
  22. return self;
  23. }
  24.  
  25. + (instancetype)shopWithDict:(NSDictionary *)dict
  26. {
  27. // 这里要用self
  28. return [[self alloc] initWithDict:dict];
  29. }
  30.  
  31. :自定义一个View,引入模型类,并且创建模型类的属性
  32.  
  33. @class XMGShop;
  34.  
  35. /** 商品模型 */
  36. @property (nonatomic, strong) XMGShop *shop;
  37.  
  38. :实现文件中,定义相应的控件属性
  39. /** 图片 */
  40. @property (nonatomic, weak) UIImageView *iconView;
  41.  
  42. /** 名字 */
  43. @property (nonatomic, weak) UILabel *nameLabel;
  44.  
  45. :实现自定义View的相应方法
  46. - (instancetype)init
  47. {
  48. if (self = [super init]) {
  49. // 添加一个图片
  50. UIImageView *iconView = [[UIImageView alloc] init];
  51. [self addSubview:iconView];
  52. self.iconView = iconView;
  53.  
  54. // 添加一个文字
  55. UILabel *nameLabel = [[UILabel alloc] init];
  56. nameLabel.textAlignment = NSTextAlignmentCenter;
  57. [self addSubview:nameLabel];
  58. self.nameLabel = nameLabel;
  59. }
  60. return self;
  61. }
  62.  
  63. /**
  64. * 这个方法专门用来布局子控件,设置子控件的frame
  65. */
  66. - (void)layoutSubviews
  67. {
  68. // 一定要调用super方法
  69. [super layoutSubviews];
  70.  
  71. CGFloat shopW = self.frame.size.width;
  72. CGFloat shopH = self.frame.size.height;
  73.  
  74. self.iconView.frame = CGRectMake(, , shopW, shopW);
  75. self.nameLabel.frame = CGRectMake(, shopW, shopW, shopH - shopW);
  76. }
  77.  
  78. -(void)setShop:(XMGShop *)shop
  79. {
  80. _shop = shop;
  81.  
  82. self.iconView.image = [UIImage imageNamed:shop.icon];
  83. self.nameLabel.text = shop.name;
  84. }
  85. :定义一个数组用来保存读取出来的plist数据
  86. @property (nonatomic, strong) NSMutableArray *shops;
  87.  
  88. :使用懒加载的方式加载plist文件,并且放到模型中
  89. // 懒加载
  90. // 1.第一次用到时再去加载
  91. // 2.只会加载一次
  92. - (NSMutableArray *)shops
  93. {
  94. if (_shops == nil) {
  95. // 创建"模型数组"
  96. _shops = [NSMutableArray array];
  97.  
  98. // 获得plist文件的全路径
  99. NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
  100.  
  101. // 从plist文件中加载一个数组对象(这个数组中存放的都是NSDictionary对象)
  102. NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
  103.  
  104. // 将 “字典数组” 转换为 “模型数据”
  105. for (NSDictionary *dict in dictArray) { // 遍历每一个字典
  106. // 将 “字典” 转换为 “模型”
  107. XMGShop *shop = [XMGShop shopWithDict:dict];
  108.  
  109. // 将 “模型” 添加到 “模型数组中”
  110. [_shops addObject:shop];
  111. }
  112. }
  113. return _shops;
  114. }
  115.  
  116. :使用View
  117. // 创建一个商品父控件
  118. XMGShopView *shopView = [[XMGShopView alloc] init];
  119. shopView.frame = CGRectMake(shopX, shopY, shopW, shopH);
  120. // 将商品父控件添加到shopsView中
  121. [self.shopsView addSubview:shopView];
  122.  
  123. /**
  124.  
  125. NSDictionary *dict = nil; // 从其他地方加载的字典
  126.  
  127. XMGShop *shop = [XMGShop shopWithDict:dict];
  128.  
  129. XMGShopView *shopView = [[XMGShopView alloc] init];
  130. shopView.shop = shop;
  131. shopView.frame = CGRectMake(0, 0, 70, 100);
  132. [self.view addSubview:shopView];
  133.  
  134. // 扩展性差
  135. // 扩展好的体现:即使改变了需求。我们也不需要动大刀子
  136. */
  137.  
  138. /******************************************************************************/

五:initWithFrame

  1. 五:initWithFrame
  2. :在上一步的基础上只要修改init方法为
  3. /** init方法内部会自动调用initWithFrame:方法 */
  4. - (instancetype)initWithFrame:(CGRect)frame
  5. {
  6. if (self = [super initWithFrame:frame]) {
  7. // 添加一个图片
  8. UIImageView *iconView = [[UIImageView alloc] init];
  9. [self addSubview:iconView];
  10. self.iconView = iconView;
  11.  
  12. // 添加一个文字
  13. UILabel *nameLabel = [[UILabel alloc] init];
  14. nameLabel.textAlignment = NSTextAlignmentCenter;
  15. [self addSubview:nameLabel];
  16. self.nameLabel = nameLabel;
  17. }
  18. return self;
  19. }
  20.  
  21. :最后设置数据的时候也可以使用下面的方法实现View的创建
  22. XMGShopView *shopView = [[XMGShopView alloc] initWithFrame:CGRectMake(shopX, shopY, shopW, shopH)];
  23.  
  24. /******************************************************************************/

六:MVC

  1. 六:MVC
  2.  
  3. :model
  4. @interface XMGShop : NSObject
  5. /** 名字 */
  6. @property (nonatomic, copy) NSString *name;
  7. /** 图标 */
  8. @property (nonatomic, copy) NSString *icon;
  9. /** 通过一个字典来初始化模型对象 */
  10. - (instancetype)initWithDict:(NSDictionary *)dict;
  11.  
  12. /** 通过一个字典来创建模型对象 */
  13. + (instancetype)shopWithDict:(NSDictionary *)dict;
  14. @end
  15.  
  16. @implementation XMGShop
  17.  
  18. - (instancetype)initWithDict:(NSDictionary *)dict
  19. {
  20. if (self = [super init]) {
  21. self.name = dict[@"name"];
  22. self.icon = dict[@"icon"];
  23. }
  24. return self;
  25. }
  26.  
  27. + (instancetype)shopWithDict:(NSDictionary *)dict
  28. {
  29. // 这里要用self
  30. return [[self alloc] initWithDict:dict];
  31. }
  32.  
  33. @end
  34.  
  35. :view
  36. @class XMGShop;
  37.  
  38. @interface XMGShopView : UIView
  39. /** 商品模型 */
  40. @property (nonatomic, strong) XMGShop *shop;
  41.  
  42. - (instancetype)initWithShop:(XMGShop *)shop;
  43. + (instancetype)shopViewWithShop:(XMGShop *)shop;
  44. + (instancetype)shopView;
  45. @end
  46.  
  47. @interface XMGShopView()
  48. /** 图片 */
  49. @property (nonatomic, weak) UIImageView *iconView;
  50.  
  51. /** 名字 */
  52. @property (nonatomic, weak) UILabel *nameLabel;
  53. @end
  54.  
  55. @implementation XMGShopView
  56.  
  57. - (instancetype)initWithShop:(XMGShop *)shop
  58. {
  59. if (self = [super init]) {
  60. self.shop = shop;
  61. }
  62. return self;
  63. }
  64.  
  65. + (instancetype)shopViewWithShop:(XMGShop *)shop
  66. {
  67. return [[self alloc] initWithShop:shop];
  68. }
  69.  
  70. + (instancetype)shopView
  71. {
  72. return [[self alloc] init];
  73. }
  74.  
  75. /** init方法内部会自动调用initWithFrame:方法 */
  76. - (instancetype)initWithFrame:(CGRect)frame
  77. {
  78. if (self = [super initWithFrame:frame]) {
  79. // 添加一个图片
  80. UIImageView *iconView = [[UIImageView alloc] init];
  81. [self addSubview:iconView];
  82. self.iconView = iconView;
  83.  
  84. // 添加一个文字
  85. UILabel *nameLabel = [[UILabel alloc] init];
  86. nameLabel.textAlignment = NSTextAlignmentCenter;
  87. [self addSubview:nameLabel];
  88. self.nameLabel = nameLabel;
  89. }
  90. return self;
  91. }
  92.  
  93. /**
  94. * 当前控件的frame发生改变的时候就会调用
  95. * 这个方法专门用来布局子控件,设置子控件的frame
  96. */
  97. - (void)layoutSubviews
  98. {
  99. // 一定要调用super方法
  100. [super layoutSubviews];
  101.  
  102. CGFloat shopW = self.frame.size.width;
  103. CGFloat shopH = self.frame.size.height;
  104.  
  105. self.iconView.frame = CGRectMake(, , shopW, shopW);
  106. self.nameLabel.frame = CGRectMake(, shopW, shopW, shopH - shopW);
  107. }
  108.  
  109. - (void)setShop:(XMGShop *)shop
  110. {
  111. _shop = shop;
  112.  
  113. self.iconView.image = [UIImage imageNamed:shop.icon];
  114. self.nameLabel.text = shop.name;
  115. }
  116.  
  117. @end
  118.  
  119. controller
  120.  
  121. @property (nonatomic, strong) NSMutableArray *shops;
  122.  
  123. // 懒加载
  124. // 1.第一次用到时再去加载
  125. // 2.只会加载一次
  126. - (NSMutableArray *)shops
  127. {
  128. if (_shops == nil) {
  129. // 创建"模型数组"
  130. _shops = [NSMutableArray array];
  131.  
  132. // 获得plist文件的全路径
  133. NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
  134.  
  135. // 从plist文件中加载一个数组对象(这个数组中存放的都是NSDictionary对象)
  136. NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
  137.  
  138. // 将 “字典数组” 转换为 “模型数据”
  139. for (NSDictionary *dict in dictArray) { // 遍历每一个字典
  140. // 将 “字典” 转换为 “模型”
  141. XMGShop *shop = [XMGShop shopWithDict:dict];
  142.  
  143. // 将 “模型” 添加到 “模型数组中”
  144. [_shops addObject:shop];
  145. }
  146. }
  147. return _shops;
  148. }
  149.  
  150. // 创建一个商品父控件
  151. XMGShopView *shopView = [XMGShopView shopViewWithShop:self.shops[index]];
  152. // 设置frame
  153. shopView.frame = CGRectMake(shopX, shopY, shopW, shopH);
  154. // 将商品父控件添加到shopsView中
  155. [self.shopsView addSubview:shopView];
  156.  
  157. /******************************************************************************/

七:Xib的使用

  1. 七:XIB
  2.  
  3. :xibView
  4. /** 商品模型 */
  5. @property (nonatomic, strong) XMGShop *shop;
  6. + (instancetype)shopViewWithShop:(XMGShop *)shop;
  7.  
  8. + (instancetype)shopViewWithShop:(XMGShop *)shop
  9. {
  10. // self == XMGShopView
  11. // NSStringFromClass(self) == @"XMGShopView"
  12. XMGShopView *shopView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
  13. shopView.shop = shop;
  14. return shopView;
  15. }
  16.  
  17. - (void)setShop:(XMGShop *)shop
  18. {
  19. _shop = shop;
  20.  
  21. UIImageView *iconView = (UIImageView *)[self viewWithTag:];
  22. iconView.image = [UIImage imageNamed:shop.icon];
  23.  
  24. UILabel *nameLabel = (UILabel *)[self viewWithTag:];
  25. nameLabel.text = shop.name;
  26. }
  27.  
  28. :控制器中设置数据
  29. // 从xib中加载一个商品控件
  30. XMGShopView *shopView = [XMGShopView shopViewWithShop:self.shops[index]];
  31. // 设置frame
  32. shopView.frame = CGRectMake(shopX, shopY, shopW, shopH);
  33. // 添加商品控件
  34. [self.shopsView addSubview:shopView];

八:牛逼框架MJExtension使用

  1. /******************************************************************************/
  2. 八:MJExtension
  3. :是一套“字典和模型之间互相转换”的轻量级框架,模型属性
  4. /**
  5. * 微博文本内容
  6. */
  7. @property (copy, nonatomic) NSString *text;
  8.  
  9. /**
  10. * 微博作者
  11. */
  12. @property (strong, nonatomic) User *user;
  13.  
  14. /**
  15. * 转发的微博
  16. */
  17. @property (strong, nonatomic) Status *retweetedStatus;
  18.  
  19. /**
  20. * 存放着某一页微博数据(里面都是Status模型)
  21. */
  22. @property (strong, nonatomic) NSMutableArray *statuses;
  23.  
  24. /**
  25. * 总数
  26. */
  27. @property (assign, nonatomic) NSNumber *totalNumber;
  28.  
  29. /**
  30. * 上一页的游标
  31. */
  32. @property (assign, nonatomic) long long previousCursor;
  33.  
  34. /**
  35. * 下一页的游标
  36. */
  37. @property (assign, nonatomic) long long nextCursor;
  38.  
  39. /**
  40. * 名称
  41. */
  42. @property (copy, nonatomic) NSString *name;
  43.  
  44. /**
  45. * 头像
  46. */
  47. @property (copy, nonatomic) NSString *icon;
  48.  
  49. :对应方法的实现
  50. /**
  51. MJ友情提醒:
  52. 1.MJExtension是一套“字典和模型之间互相转换”的轻量级框架
  53. 2.MJExtension能完成的功能
  54. * 字典 --> 模型
  55. * 模型 --> 字典
  56. * 字典数组 --> 模型数组
  57. * 模型数组 --> 字典数组
  58. 3.具体用法主要参考 main.m中各个函数 以及 "NSObject+MJKeyValue.h"
  59. 4.希望各位大神能用得爽
  60. */
  61.  
  62. #import <Foundation/Foundation.h>
  63. #import "MJExtension.h"
  64. #import "User.h"
  65. #import "Status.h"
  66. #import "StatusResult.h"
  67.  
  68. /**
  69. * 简单的字典 -> 模型
  70. */
  71. void keyValues2object()
  72. {
  73. // 1.定义一个字典
  74. NSDictionary *dict = @{
  75. @"name" : @"Jack",
  76. @"icon" : @"lufy.png",
  77. };
  78.  
  79. // 2.将字典转为User模型
  80. User *user = [User objectWithKeyValues:dict];
  81.  
  82. // 3.打印User模型的属性
  83. NSLog(@"name=%@, icon=%@", user.name, user.icon);
  84. }
  85.  
  86. /**
  87. * 复杂的字典 -> 模型 (模型里面包含了模型)
  88. */
  89. void keyValues2object2()
  90. {
  91. // 1.定义一个字典
  92. NSDictionary *dict = @{
  93. @"text" : @"是啊,今天天气确实不错!",
  94.  
  95. @"user" : @{
  96. @"name" : @"Jack",
  97. @"icon" : @"lufy.png"
  98. },
  99.  
  100. @"retweetedStatus" : @{
  101. @"text" : @"今天天气真不错!",
  102.  
  103. @"user" : @{
  104. @"name" : @"Rose",
  105. @"icon" : @"nami.png"
  106. }
  107. }
  108. };
  109.  
  110. // 2.将字典转为Status模型
  111. Status *status = [Status objectWithKeyValues:dict];
  112.  
  113. // 3.打印status的属性
  114. NSString *text = status.text;
  115. NSString *name = status.user.name;
  116. NSString *icon = status.user.icon;
  117. NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
  118.  
  119. // 4.打印status.retweetedStatus的属性
  120. NSString *text2 = status.retweetedStatus.text;
  121. NSString *name2 = status.retweetedStatus.user.name;
  122. NSString *icon2 = status.retweetedStatus.user.icon;
  123. NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
  124. }
  125.  
  126. /**
  127. * 复杂的字典 -> 模型 (模型的数组属性里面又装着模型)
  128. */
  129. void keyValues2object3()
  130. {
  131. // 1.定义一个字典
  132. NSDictionary *dict = @{
  133. @"statuses" : @[
  134. @{
  135. @"text" : @"今天天气真不错!",
  136.  
  137. @"user" : @{
  138. @"name" : @"Rose",
  139. @"icon" : @"nami.png"
  140. }
  141. },
  142.  
  143. @{
  144. @"text" : @"明天去旅游了",
  145.  
  146. @"user" : @{
  147. @"name" : @"Jack",
  148. @"icon" : @"lufy.png"
  149. }
  150. },
  151.  
  152. @{
  153. @"text" : @"嘿嘿,这东西不错哦!",
  154.  
  155. @"user" : @{
  156. @"name" : @"Jim",
  157. @"icon" : @"zero.png"
  158. }
  159. }
  160.  
  161. ],
  162.  
  163. ",
  164.  
  165. ",
  166.  
  167. "
  168. };
  169.  
  170. // 2.将字典转为StatusResult模型
  171. StatusResult *result = [StatusResult objectWithKeyValues:dict];
  172.  
  173. // 3.打印StatusResult模型的简单属性
  174. NSLog(@"totalNumber=%d, previousCursor=%lld, nextCursor=%lld", result.totalNumber, result.previousCursor, result.nextCursor);
  175.  
  176. // 4.打印statuses数组中的模型属性
  177. for (Status *status in result.statuses) {
  178. NSString *text = status.text;
  179. NSString *name = status.user.name;
  180. NSString *icon = status.user.icon;
  181. NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
  182. }
  183. }
  184.  
  185. /**
  186. * 字典数组 -> 模型数组
  187. */
  188. void keyValuesArray2objectArray()
  189. {
  190. // 1.定义一个字典数组
  191. NSArray *dictArray = @[
  192. @{
  193. @"name" : @"Jack",
  194. @"icon" : @"lufy.png",
  195. },
  196.  
  197. @{
  198. @"name" : @"Rose",
  199. @"icon" : @"nami.png",
  200. },
  201.  
  202. @{
  203. @"name" : @"Jim",
  204. @"icon" : @"zero.png",
  205. }
  206. ];
  207.  
  208. // 2.将字典数组转为User模型数组
  209. NSArray *userArray = [User objectArrayWithKeyValuesArray:dictArray];
  210.  
  211. // 3.打印userArray数组中的User模型属性
  212. for (User *user in userArray) {
  213. NSLog(@"name=%@, icon=%@", user.name, user.icon);
  214. }
  215. }
  216.  
  217. /**
  218. * 模型 -> 字典
  219. */
  220. void object2keyValues()
  221. {
  222. // 1.新建模型
  223. User *user = [[User alloc] init];
  224. user.name = @"Jack";
  225. user.icon = @"lufy.png";
  226.  
  227. Status *status = [[Status alloc] init];
  228. status.user = user;
  229. status.text = @"今天的心情不错!";
  230.  
  231. // 2.将模型转为字典
  232. // NSDictionary *dict = [status keyValues];
  233. NSDictionary *dict = status.keyValues;
  234. NSLog(@"%@", dict);
  235. }
  236.  
  237. /**
  238. * 模型数组 -> 字典数组
  239. */
  240. void objectArray2keyValuesArray()
  241. {
  242. // 1.新建模型数组
  243. User *user1 = [[User alloc] init];
  244. user1.name = @"Jack";
  245. user1.icon = @"lufy.png";
  246.  
  247. User *user2 = [[User alloc] init];
  248. user2.name = @"Rose";
  249. user2.icon = @"nami.png";
  250.  
  251. User *user3 = [[User alloc] init];
  252. user3.name = @"Jim";
  253. user3.icon = @"zero.png";
  254.  
  255. NSArray *userArray = @[user1, user2, user3];
  256.  
  257. // 2.将模型数组转为字典数组
  258. NSArray *dictArray = [User keyValuesArrayWithObjectArray:userArray];
  259. NSLog(@"%@", dictArray);
  260. }
  261.  
  262. int main(int argc, const char * argv[])
  263. {
  264. @autoreleasepool {
  265. // 简单的字典 -> 模型
  266. keyValues2object();
  267.  
  268. // 复杂的字典 -> 模型 (模型里面包含了模型)
  269. keyValues2object2();
  270.  
  271. // 复杂的字典 -> 模型 (模型的数组属性里面又装着模型)
  272. keyValues2object3();
  273.  
  274. // 字典数组 -> 模型数组
  275. keyValuesArray2objectArray();
  276.  
  277. // 模型转字典
  278. object2keyValues();
  279.  
  280. // 模型数组 -> 字典数组
  281. objectArray2keyValuesArray();
  282. }
  283. ;
  284. }

iOS开发——笔记篇&关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结的更多相关文章

  1. ios开发——笔记篇

    :开关 BOOL isopen = !isopen; //View @property (nonatomic, assign) BOOL open;//模型属性 self.group.open = ! ...

  2. iOS开发UI篇—字典转模型

    iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...

  3. iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)

    iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist) 一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存 ...

  4. IOS开发笔记(4)数据离线缓存与读取

    IOS开发笔记(4)数据离线缓存与读取 分类: IOS学习2012-12-06 16:30 7082人阅读 评论(0) 收藏 举报 iosiOSIOS 方法一:一般将服务器第一次返回的数据保存在沙盒里 ...

  5. iOS开发UI篇—简单的浏览器查看程序

    iOS开发UI篇—简单的浏览器查看程序 一.程序实现要求 1.要求 2. 界面分析 (1) 需要读取或修改属性的控件需要设置属性 序号标签 图片 图片描述 左边按钮 右边按钮 (2) 需要监听响应事件 ...

  6. iOS开发UI篇—从代码的逐步优化看MVC

    iOS开发UI篇—从代码的逐步优化看MVC 一.要求 要求完成下面一个小的应用程序. 二.一步步对代码进行优化 注意:在开发过程中,优化的过程是一步一步进行的.(如果一个人要吃五个包子才能吃饱,那么他 ...

  7. iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

    iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...

  8. iOS开发数据库篇—SQLite简单介绍

    iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等. 说明:离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式 (1 ...

  9. iOS开发UI篇—xib的简单使用

    iOS开发UI篇—xib的简单使用 一.简单介绍 xib和storyboard的比较,一个轻量级一个重量级. 共同点: 都用来描述软件界面 都用Interface Builder工具来编辑 不同点: ...

随机推荐

  1. HDU 2056 Rectangles

    Rectangles Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. 16、编写适应多个API Level的APK

     确认您是否需要多apk支持 当你试图创建一个支持跨多代android系统的应用程序时,很自然的 你希望你的应用程序可以在新设备上使用新特性,并且不会牺牲向后兼 容.刚开始的时候认为通过创建多个ap ...

  3. BLOCK 死循环

    __weak typeof(self) weakSelf = self; myObj.myBlock =  ^{     __strong typeof(self) strongSelf = weak ...

  4. 转载--详解tomcat配置

    http://www.importnew.com/17124.html  原文链接 几乎所有容器类型的应用都会包含一个名为 server.xml 的文件结构.基本上,其中的每个元数据或者配置都是容器完 ...

  5. python学习之subprocess模块

    subprocess.Popen 这个模块主要就提供一个类Popen: class subprocess.Popen( args, bufsize=0, executable=None, stdin= ...

  6. linux 下的进程管理工具 supervisor

    在linux下监控进程: 1)yum install python-setuptools   linux下的python安装工具 2)easy_install supervisor     安装sup ...

  7. 【Spark学习】Apache Spark集群硬件配置要求

    Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4135912.html 目录 存 ...

  8. Gym 100507C Zhenya moves from parents (线段树)

    Zhenya moves from parents 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/C Description Z ...

  9. Spring properties dependency checking

    In Spring,you can use dependency checking feature to make sure the required properties have been set ...

  10. 一个word合并项目的分布式架构设计

    一个word合并项目的分布式架构设计 项目背景与问题起源 我们要给一个客户做word生成报告以及报告合并的工作,要合并的报告非常多,而且每个报告也比较大,一个多的报告大概有200页以上.我们用c#操作 ...