iPhone读取和写入plist文件
2011-10-12 19:52
441 查看
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.
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.
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.
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 :)
以下代码在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 :)
相关文章推荐
- iPhone读取和写入plist文件
- iPhone读取和写入plist文件
- iPhone读取和写入plist文件
- iPhone读取和写入plist文件
- IOS复习Plist文件的读取和写入
- iphone怎样把音频文件写入本地文件,然后从本地文件读取到内存,进行播放音频?
- Objc运行时读取和写入plist文件遇到的问题
- (有码)读取和写入自定义对象 plist文件解析
- Objc运行时读取和写入plist文件遇到的问题
- iOS 字典和数组写入plist文件存到应用沙盒当中(含读取)
- iPhone 不能读取plist文件!?
- iOS 创建.写入.读取plist文件
- Iphone开发(十一)从plist文件读取列表数据并添加索引
- Objc运行时读取和写入plist文件遇到的问题
- iPhone开发-创建、读取、写入文件 今天的东西是如何在iPhoneOS下创建、删除、读取、写入文件 创建与删除: //创建文件管理器 NSFileManager *fileManager =
- iPhone开发之文件创建、删除、读取、写入
- iPhone 下创建、删除、读取、写入文件
- 对象类型的本地写入---plist文件创建以及读取
- iOS开发读取plist文件、iphone中plist文件的
- iPhone开发之UITableView的小试牛刀 利用MVC模式从plist文件读取数据并字典转模型的格式实现