当前位置:编程学习 > C/C++ >>

plist文件读写

[cpp]  - (void)viewDidLoad 

    [super viewDidLoad]; 
     
    NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b", nil]; 
    NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"c",@"4",@"d", nil]; 
     
    // 获取应用程序沙盒的Documents目录  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *plistPath = [paths objectAtIndex:0]; 
     
    // plist文件完整路径  
    NSString *filename=[plistPath stringByAppendingPathComponent:@"test.plist"]; 
     
    // *写入(原有数据被覆盖)  
    [dictionary1 writeToFile:filename atomically:YES]; 
    [dictionary2 writeToFile:filename atomically:YES]; 
     
    // *读取  
    NSDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filename]; 
    NSLog(@"%@", dic); 

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b", nil];
    NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"c",@"4",@"d", nil];
 
    // 获取应用程序沙盒的Documents目录
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
   
    // plist文件完整路径
    NSString *filename=[plistPath stringByAppendingPathComponent:@"test.plist"];
   
    // *写入(原有数据被覆盖)
    [dictionary1 writeToFile:filename atomically:YES];
    [dictionary2 writeToFile:filename atomically:YES];
   
    // *读取
    NSDictionary *dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", dic);
}

 

\

 

结论:重复对同一plist写入数据,后一次写入的数据将覆盖前一次写入的数据。


 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,