封裝傳入plist文件名 和 類名 作爲參數的方法

-(NSArray )getAndParseWithPlistFile:(NSString )fileName withClass:(Class)modelClass {
//從bundle中獲取文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];

NSArray *dataArray = [NSArray arrayWithContentOfFile:filePath];
;
// 解析
//創建一個臨時可變數組
NSMutableArray *mutableArray = [NSMutableArray array];
for (NSDictionary *dataDic in dataArray) {
//一個一個解析:kvc
id newInstance = [modelClass alloc]init];
[newInstance setValuesForKeysWithDictionary:dataDic];

//將解析玩的分類模型添加到可變數組
[mutableArray addobject:newInstance];
}
return [mutableArray copy];
}
//僅作爲課堂筆記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章