本地文件.json .plist文件是较为常用的存储本地数据的文件,对这些文件的操作也是一种常用的基础。

本文同时提供初始化变量的比较标准的写法,如果你有更好的初始化变量的写法,欢迎留言提醒我。

.json文件的数据获取需要通过赋值NSData,再通过NSJSONSerialization 方法将NSData数据转成NSArray 或NSDictionary进行使用。

.plist文件的数据可以直接进行访问。

//获取本地location.json文件内容
@propertynonatomic, strong) NSArray *locData;
- (NSArray *)locData {
_locData) {
NSDataNSDatadataWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"location"ofType:@"json"]];
NSArrayNSJSONSerializationJSONObjectWithData:JSONData options:NSJSONReadingAllowFragmentserror:nil];
NSMutableArrayNSMutableArrayarray];
NSDictionary *dict in dataArray) {
XCFLocationXCFLocationlocationWithDict:dict];
addObject:loc];
        }
_locData = newArray;
    }
_locData;
}
//获取本地keywords.plist文件内容
@propertynonatomic, strong) NSArray *hotSearchWords;
- (NSArray *)hotSearchWords {
_hotSearchWords) {
NSDictionaryNSDictionarydictionaryWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"keywords"ofType:@"plist"]];
NSArrayNSArrayarrayWithArray:dataDict[@"content"][@"keywords"]];
NSMutableArrayNSMutableArrayarray];
NSString *word in array) {
addObject:word];
        }
_hotSearchWords = mArray;
    }
_hotSearchWords;
}
 
//写入.plist文件
NSString *filename = [[NSBundlemainBundle] pathForResource:@"keywords"ofType:@"plist”];
NSDictionaryNSDictionarydictionaryWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"keywords"ofType:@"plist"]];
[dataDict"add some content""c_key"]; 
[dataDict writeToFile:filename atomically:YES];