美文网首页
iOS读取本地.json和.plist文件

iOS读取本地.json和.plist文件

作者: 关羽007 | 来源:发表于2019-03-12 15:09 被阅读0次

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

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

//获取本地location.json文件内容

@property (nonatomic, strong) NSArray *locData;

- (NSArray *)locData {

if (!_locData) {

 NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"location" ofType:@"json"]];

 NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];

 NSMutableArray *newArray = [NSMutableArray array];

for (NSDictionary *dict in dataArray) {

 XCFLocation *loc = [XCFLocation locationWithDict:dict];

[newArray addObject:loc];

        }

 _locData = newArray;

    }

return _locData;

}

//获取本地keywords.plist文件内容

@property (nonatomic, strong) NSArray *hotSearchWords;

- (NSArray *)hotSearchWords {

if(!_hotSearchWords) {

 NSDictionary *dataDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"keywords"ofType:@"plist"]];

 NSArray *array = [NSArray arrayWithArray:dataDict[@"content"][@"keywords"]];

 NSMutableArray *mArray = [NSMutableArray array];

for (NSString *word in array) {

[mArray addObject:word];

        }

 _hotSearchWords = mArray;

    }

return _hotSearchWords;

}

//写入.plist文件

NSString *filename = [[NSBundle mainBundle] pathForResource:@"keywords" ofType:@"plist”];

NSDictionary *dataDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"keywords" ofType:@"plist"]];

[dataDictsetObject:@"add some content"forKey:@"c_key"]; 

[dataDict writeToFile:filename atomically:YES];

相关文章

网友评论

      本文标题:iOS读取本地.json和.plist文件

      本文链接:https://www.haomeiwen.com/subject/gecnpqtx.html