码迷,mamicode.com
首页 > Web开发 > 详细

把txt文件中的json字符串写到plist文件中

时间:2014-08-04 13:38:27      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   文件   数据   for   cti   

- (void)json2Plist
{
    NSString *filePath = [self applicationDocumentsDirectoryFileName:@"json"];
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    
    //第一次添加数据时,数组为空
    if (tempArray.count == 0) {
        tempArray = [NSMutableArray array];
    }
    
  //文件名(utf-8编码) NSString
*path = [[NSBundle mainBundle] pathForResource:@"city2" ofType:@"txt"]; NSString *contents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSArray *list = [contents componentsSeparatedByString:@","]; for (NSString *tempStr in list) { NSDictionary *dict = [tempStr propertyListFromStringsFileFormat]; [tempArray addObject:dict]; } BOOL success = [tempArray writeToFile:filePath atomically:YES]; NSLog(@"success:%d",success); } #pragma mark - 获取沙盒中的Plist文件路径 - (NSString *)applicationDocumentsDirectoryFileName:(NSString *)fileName { NSString *filePath; //沙盒中的Document文件夹 NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; //获取传来的Plist文件名 NSString *fileNameStr = [NSString stringWithFormat:@"%@.plist",fileName]; filePath = [documentDirectory stringByAppendingPathComponent:fileNameStr]; return filePath; }
//txt文本格式示例:
{id = 110000;contents = "北京市";},{id = 120000;contents = "天津市";},{id = 130100;contents = "石家庄市";},{id = 654300;contents = "阿勒泰地区";}

 

把txt文件中的json字符串写到plist文件中,布布扣,bubuko.com

把txt文件中的json字符串写到plist文件中

标签:style   blog   color   io   文件   数据   for   cti   

原文地址:http://www.cnblogs.com/hw140430/p/3889638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!