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"]];
         

       




iphone-plist-tutorial

Many people transfer data to and from web services to the iPhone via JSON. This is a much better way than using XML. Parsing XML on the iPhone just plain sucks. It’s overly complicated and super confusing. Currently the YouVersion app,Bible, uses JSON to transfer everything back and forth from the server and it all works great.

So you’re probably thinking, “So, great I’ll just keep doing that.” I did too until I went to one of the Apple iPhone Tech Talks. I learned a ton about how to optomize iPhone apps. One of the big things they hit on was using plists to transfer data back and forth instead of JSON or XML.

The huge benefit to using plists over JSON is that you don’t have to parse them, they are 100% native. You can initialize an de>NSDictionaryde> or de>NSArrayde> with just one method.

de>NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSArray *array = [NSArray arrayWithContentsOfURL:plistURL];de>

This is a super simple and easy way to do this. I would recommend using de>NSURLConnectionde> to pull down your plist file to the temp directory and the run de>initWithContentsOfFile:de> instead of using thede>initWithContentsOfURL:de>. de>NSURLConnectionde> provides some great added features, like being asynchronous and handeling HTTP auth, etc.

Feel free to grab the iPhone Plist Tutorial project source code on GitHub at http://github.com/samsoffes/iphone-plist. It’s pretty straight forward. I tried to make it as simple as possible.

Now What?

You’re probably thinking, “Great, so how do I get my data into a plist?” Well, all you have to do is google up a parser for your language. I started writing one for PHP because that is what I use. It takes a PHP array (associative or nonassociative) and converts it into a plist string. You can find it in the helpers folder in this repo.

Anyone Else Doing This?

I wish. It would be great if more web services offered a plist version of their API. In the new YouVersion API, we will be offering a plist format. Hopefully as the iPhone grows in popularity, this will become more and more common.

If you don’t really have a choice, at least use JSON. Here is a great library for JSON that I currently use in Bible. The next version of Bible will be all plist using the new YouVersion API. I can’t wait :)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值