ios-plist文件读写操作(2)
打印出来的结果:
 
[cpp]
PlistDemo[6822:f803] { 
    jack =     { 
        age = 22; 
        "phone_num" = 13801111111; 
    }; 
    tom =     { 
        age = 36; 
        "phone_num" = 13901111111; 
    }; 
} 
 
这样就把数据读取出来了。
 
4、创建和写入plist文件
在开发过程中,有时候需要把程序的一些配置保存下来,或者游戏数据等等。 这时候需要写入Plist数据。
写入的plist文件会生成在对应程序的沙盒目录里。
接着上面读取plist数据的代码,加入了写入数据的代码,
 
[cpp]
<strong>- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //读取plist 
 
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"]; 
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; 
    NSLog(@"%@", data); 
     
    //添加一项内容 
    [data setObject:@"add some content" forKey:@"c_key"]; 
     
    //获取应用程序沙盒的Documents目录 
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *plistPath1 = [paths objectAtIndex:0]; 
     
    //得到完整的文件名 
    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"test.plist"]; 
   //输入写入 
    [data writeToFile:filename atomically:YES]; 
     
    //那怎么证明我的数据写入了呢?读出来看看 
    NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename]; 
    NSLog(@"%@", data1); 
     
     
    // Do any additional setup after loading the view, typically from a nib. 
} 
</strong> 
相关新闻>>
- 发表评论
- 
				
- 最新评论 进入详细评论页>>









![[iPhone中级]iPhone团购信息客户端的开发 (二)](/uploads/allimg/131022/200H11235-0-lp.png)
