iOS新手学习

IOS中字典数组的模型封装

2016-03-26  本文已影响1618人  南波万_

用模型取代字典数组的好处

```objc

NSString *name = dict[@"name"];
dict[@"name"] = @"jordan";
```

字典转模型过程

import <Foundation/Foundation.h>

@interface shop : NSObject
@interface property (strong, nanotomic) NSString * name;
@interface property (strong, nanotomic) NSString * icon;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)initWithDict:(NSDictionary *)dict;
@end
```

pragma mark - 加载字典数组

  - (NSArray *) shops{
   if (_shop == nil){
      //1、加载一个字典数组
      NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"]];
      //2、将字典数组转换为字典模型
      NSMutableArray *shopArray = [NSMutableArray array];
      for (NSDictionary *dict in dictArray){
      shop *s = [shop shopWithDict:dict];
      [shopArray addObject:shop];
      }
      //3、赋值
      _shop = shopArray;
    }
  return _shops;
  }

  ```
上一篇 下一篇

猜你喜欢

热点阅读