1.懶加載
// 加載plist數(shù)據(jù)(比較大)
// 懶加載:用到時再去加載逮矛,而且也只加載一次
- (NSArray *)shops
{
if (_shops == nil) {
// 加載一個字典數(shù)組
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"]];
NSMutableArray *shopArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
XMGShop *shop = [XMGShop shopWithDict:dict];
[shopArray addObject:shop];
}
_shops = shopArray;
}
return _shops;
}
2.模型類
(1).h文件
#import <Foundation/Foundation.h>
@interface XMGShop : NSObject
/** 商品名稱 */
@property (nonatomic, strong) NSString *name;
/** 圖標 */
@property (nonatomic, strong) NSString *icon;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)shopWithDict:(NSDictionary *)dict;
@end
(2).m文件
#import "XMGShop.h"
@implementation XMGShop
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
+ (instancetype)shopWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end