在Mac OS X的Cocoa,NeXTSTEP和GNUstep编程框架中,属性列表(Property List)文件是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.pList,因此通常被称为pList文件。
PList文件通常用于储存用户设置,也可以用于存储捆绑的信息,该功能在旧式的Mac OS中是由资源分支提供的。
使用mac os 和Core Foundation中的property List接口我们可以在层式的对象和xml文件之间进行转换。我们可以把xml文件存储起来以后再把它以对象的形式读取出来。这里我们来具体讨论下property List和他们的表现形式,以及如何在编程过程中使用他们。
这里我想提到一下NSUserDefault,它其实也是以property List 的形式来存储的,但是它有限制,比如说NScolor和NSFont等类型式不能够直接存储的,我们必须要转换他们,要把他们转换成NSData类型来存储,我想在另一篇文章在详细说说这个问题。
废话不多说,我们开始吧。
在编程的过程中,我们可以在项目中建立pList来存储一些变量,具体的 *** 作步骤file-new-Mac OS X-Resource-Property List。我们在项目中可以以xml形式或者source Code形式来编写。比如我们的pList原代码的形式象下面的xml一样。
<?xml version="1.0" enCoding="UTF-8"?><!DOCTYPE pList PUBliC "-//Apple//DTD PList 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><pList version="1.0"><dict> <key>name</key> <string>John Doe</string> <key>Phones</key> <array> <string>408-974-0000</string> <string>503-333-5555</string> </array></dict></pList>
接下来我们从pList中读取信息,这是在iPhone开发中的应用:
//get the pList file from bundle Nsstring *pListPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"pList"]; // build the array from the pListNSMutableArray *anArray = [[NSMutableArray alloc]initWithContentOffile:pListPath];
下面是写 *** 作:
Nsstring *error; Nsstring *rootPath = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0]; Nsstring *pListPath = [rootPath stringByAppendingPathComponent:@"Data.pList"]; NSDictionary *pListDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personname,phoneNumbers,nil] forKeys:[NSArray arrayWithObjects: @"name",@"Phones",nil]]; NSData *pListData = [nspropertyListSerialization dataFromPropertyList:pListDict format:nspropertyListXMLFormat_v1_0 errorDescription:&error]; if(pListData) { [pListData writetofile:pListPath atomically:YES]; } else { NSLog(error); [error release]; }
主要内容来自:http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/PropertyLists/QuickStartPList/QuickStartPList.HTML#//apple_ref/doc/uID/10000048i-CH4-SW5
总结以上是内存溢出为你收集整理的IOS中的存储-Plist全部内容,希望文章能够帮你解决IOS中的存储-Plist所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)