如何在 iPhone 中以编程方式创建 PLIST 文件
我正在寻找以编程方式在 Objective C 中在我的应用程序 Documents 文件夹中创建 plist 文件。我在文档目录中创建了一个文件夹:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/Data.plist", documentsDirectoryPath];
我正在尝试创建 plist 文件,它看起来像 XML 文件。 /**** 所需的 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">
<array>
<dict>
<key>height</key>
<integer>4007</integer>
<key>name</key>
<string>map</string>
<key>width</key>
<integer>6008</integer>
</dict>
</array>
</plist>
/**** 通过代码实现的文件 ****/
<?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>height</key>
<string>4007</string>
<key>name</key>
<string>map</string>
<key>width</key>
<string>6008</string>
</dict>
</plist>
所需的文件需要一个数组,数组里面有一个字典对象。我怎样才能改变这个? 我也知道如何将文件写入路径,但主要问题是如何创建 plist 文件然后读取它?
I was looking to create plist file in my application Documents folder programmatically in objective C. I created a folder in documents directory :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/Data.plist", documentsDirectoryPath];
I am trying to create plist file which will look like an XML file.
/**** Required XML File ****/
<?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">
<array>
<dict>
<key>height</key>
<integer>4007</integer>
<key>name</key>
<string>map</string>
<key>width</key>
<integer>6008</integer>
</dict>
</array>
</plist>
/****Achieved file through code ****/
<?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>height</key>
<string>4007</string>
<key>name</key>
<string>map</string>
<key>width</key>
<string>6008</string>
</dict>
</plist>
The required file needs an array and inside the array we have a dictionary object. How can I change this ?
I also know how to write the file to the path, but the major issue is how to create plist file and then read it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PLIST 文件也称为“属性列表”文件,使用 XML 格式存储数组、字典和字符串等对象。
您可以使用此代码创建、添加值以及从 plist 文件中检索值。
A PLIST file, also known as a "Property List" file, uses the XML format to store objects such as arrays, dictionaries, and strings.
You can use this code for creating, adding the values and retrieving the values from the plist file.
我认为这篇文章保存到.plist属性如果您查看其中的示例,列表会对您有所帮助。
另外,请查看 Apple 的 以编程方式创建属性列表< /a> 其他指南和示例的文档。
I think this post save to .plist properity list will help you if you look through the examples there.
Also, check out Apple's Creating Property Lists Programmatically document for other guidelines and examples.
请注意,如果您只想用一个
plist
文件来保存数据,则实际上不必创建和保存任何文件。有一种称为 NSUserDefaults 的机制。您执行类似的操作,然后阅读
准备要保存的文件,将其写入文件,在下次启动应用程序时再次读取它,自动为您完成!
阅读官方参考和官方文档。
Note that if you just want one
plist
file to keep a data, you don't really have to create and save any. There is a mechanism calledNSUserDefaults
. You do something likeand you read as in
Preparing the file to save, writing it to the file, reading it again when the app is next launched, is done automatically for you!!
Read the official reference and the official document.