您的位置:首页 > 其它

iPhone读取和写入plist文件

2013-08-19 18:20 471 查看
原文地址:iPhone读取和写入plist文件作者:xiongbiao0228

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",@"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 :)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: