iPhone读取和写入plist文件

plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法:

以下代码在Mac和iPhone中均适用。

 

写入plist文件:

  1. NSMutableDictionary *  dict  =   [   [   NSMutableDictionary   alloc   ]   initWithContentsOfFile : @ "/Sample.plist"   ] ;
  2. [  dict  setObject : @ "Yes"   forKey : @ "RestartSpringBoard"   ] ;
  3. [  dict  writeToFile : @ "/Sample.plist"   atomically : YES   ] ;

 

读取plist文件:

  1. NSMutableDictionary *  dict  =    [   [   NSMutableDictionary   alloc   ]   initWithContentsOfFile : @ "/Sample.plist"   ] ;
  2. NSString *   object   =   [  dict  objectForKey : @ "RestartSpringBoard"   ] ;


//读plist 文件 

到 NSMutableDictionary 中
    
    dictplist = [[NSMutableDictionary alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path]];

    [dictplist setObject:@"testOne" forKey:@"key1"];

    NSLog([dictplist objectForKey:@"key1"]);
    
    NSArray *array = [[NSArray alloc] initWithObjects:@"item1",@"item2",@"item3",nil];
    [dictplist setObject:array forKey:@"arrayitemtest"];
    
    [dictplist writeToFile:plistPath atomically:YES];
    
    NSString *name = [dictplist objectForKey:@"name" ];
    NSLog(name);
    
    arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"arrayitemtest"]];
    
    
    //从NSMutableDictionary 中构建 plist 文件 
     
    NSArray *array = [[NSArray alloc] initWithObjects:@"item1",@"item2",@"item3",nil];
     dictplist = [[NSMutableDictionary alloc ] init];
    [dictplist setObject:@"nameOne" forKey:@"name"];
    
    [dictplist setObject:array forKey:@"item"];
     
    [dictplist writeToFile:plistPath atomically:YES];
     //arraylist = [[NSMutableArray alloc] init];
    //[arraylist addObject:[dictplist objectForKey:@"name"]];
    
    arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"item"]];
     
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值