在控制器上懒加载
- (NSArray *)statuses { if (_statuses == nil) { // 加载plist中的字典数组 NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组 NSMutableArray *statusArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { XSPStatus *status = [XSPStatus statusWithDict:dict]; [statusArray addObject:status]; } _statuses = statusArray; } return _statuses; }
XSPStatus.h文件里
#import <Foundation/Foundation.h> @interface XSPStatus : NSObject @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) NSString *text; @property (strong, nonatomic) NSString *icon; @property (strong, nonatomic) NSString *picture; @property (assign, nonatomic, getter=isVip) BOOL vip; + (instancetype)statusWithDict:(NSDictionary *)dict; @end
XSPStatus.m文件里
#import "XSPStatus.h" @implementation XSPStatus + (instancetype)statusWithDict:(NSDictionary *)dict { XSPStatus *status = [[self alloc] init]; [status setValuesForKeysWithDictionary:dict]; return status; } @end
取出数据,结果如下:
XSPStatus *a = self.statuses[indexPath.row]; NSLog(@"%@--%@--%@--%@",a.buyCount,a.price,a.title,a.icon);