IOS中本地创建和存储Plist文件

 
1.创建plist文件
-(void)creatPlist{
    //1.创建plist文件的路径(plist文件要存储的本地路径)
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myPlist.plist"];
   
    NSLog(@"本地路径是:%@",filePath);
   
    //2.准备存储的数据
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
   
    [dic setValue:@"value1" forKey:@"key1"]; //value为字符串
   
    [dic setValue:@[@"str1",@"str2",@"str3"] forKey:@"key2"];//value为数组
   
    [dic setValue:@{@"k1":@"v1",@"k2":@"v2"} forKey:@"key3"];//value为字典
   
    //3.将准备好的数据存储到plist文件中
    [dic writeToFile:filePath atomically:YES];
    
    //**对数据进行修改
    [dic setObject:@"刘志平" forKey:@"key1"];
    [dic writeToFile:filePath atomically:YES]; //覆盖修改前的数据
}
 
 
2.读取plist文件
 
-(void)readPlist{
   
    //第一种情况:读取本地存储的plist文件
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myPlist.plist"];//要读取的plist文件的本地路径

    NSDictionary *dic1 = [NSDictionary dictionaryWithContentsOfFile:filePath];
   
    NSLog(@"dic1 = %@",dic1);


    //第二种情况:读取工程内的plist文件
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Info" ofType:@"plist"];
   
    NSDictionary *dic2 = [NSDictionary dictionaryWithContentsOfFile:path];
   
    NSLog(@"dic2 = %@",dic2);
 
}
 
 
 
 
 
 
 
 
posted @ 2016-01-23 14:12  彩虹丶直至黑白  阅读(1255)  评论(0编辑  收藏  举报