加载NSBundle文件

97 阅读1分钟
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
// 将文件数据化
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
// 对数据进行JSON格式化并返回字典形式
NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
self.dataSource = [WuliuModel modelArrayWithDictArray:dataArray];
    
+ (NSArray *)modelArrayWithDictArray:(NSArray *)array{ 
    NSMutableArray *tempArray = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        WuliuModel *model = [[[WuliuModel alloc] init]  initWithDict:dict];
        [tempArray addObject:model];
    }
    return tempArray;
}