iPhone读取和写入plist文件

iPhone读取和写入plist文件,第1张

概述  plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法: 以下代码在Mac和iPhone中均适用。   写入plist文件: NSMutableDictionary *  dict  =  [  [  NSMutableDictionary  alloc  ]  initWithContentsOfFile :@ "/Sample.plist"  ]; [  d  

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

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

 

写入pList文件:

NSMutableDictionary *  dict  =  [  [  NSMutableDictionary  alloc  ]  initWithContentsOffile :@ "/Sample.pList"  ]; [  dict  setobject :@ "Yes"  forKey :@ "RestartSpringBoard"  ]; [  dict  writetofile :@ "/Sample.pList"  atomically : YES  ];

 

读取pList文件:

NSMutableDictionary *  dict  =    [  [  NSMutableDictionary  alloc  ]  initWithContentsOffile :@ "/Sample.pList"  ]; 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",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 :)

总结

以上是内存溢出为你收集整理的iPhone读取和写入plist文件全部内容,希望文章能够帮你解决iPhone读取和写入plist文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1064871.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存