ios 如何在 iPhone 中以编程方式创建 PLIST 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6697247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:45:46  来源:igfitidea点击:

How to create PLIST files programmatically in iphone

iphoneobjective-cioscocoa-touchplist

提问by lifemoveson

I was looking to create plist file in my application Documents folder programmatically in objective C. I created a folder in documents directory :

我希望在目标 C 中以编程方式在我的应用程序 Documents 文件夹中创建 plist 文件。我在文档目录中创建了一个文件夹:

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 文件的 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">
<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 ?

所需的文件需要一个数组,在数组中我们有一个字典对象。我怎样才能改变这个?我也知道如何将文件写入路径,但主要问题是如何创建 plist 文件然后读取它?

回答by Aravindhan

A PLIST file, also known as a "Property List" file, uses the XML format to store objects such as arrays, dictionaries, and strings.

PLIST 文件,也称为“属性列表”文件,使用 XML 格式存储数组、字典和字符串等对象。

You can use this code for creating, adding the values and retrieving the values from the plist file.

您可以使用此代码从 plist 文件中创建、添加值和检索值。

//Get the documents directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {

    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"plist.plist"] ];
}

NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) {

    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else {
    // If the file doesn't exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
[data setObject:@"iPhone 6 Plus" forKey:@"value"];
[data writeToFile:path atomically:YES];

//To retrieve the data from the plist
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *value = [savedValue objectForKey:@"value"];
NSLog(@"%@",value);

回答by PengOne

I think this post save to .plist properity listwill help you if you look through the examples there.

我认为这篇文章保存到 .plist 属性列表会帮助你,如果你浏览那里的例子。

Also, check out Apple's Creating Property Lists Programmaticallydocument for other guidelines and examples.

此外,请查看 Apple 的以编程方式创建属性列表文档以获取其他指南和示例。

回答by Yuji

Note that if you just want one plistfile to keep a data, you don't really have to create and save any. There is a mechanism called NSUserDefaults. You do something like

请注意,如果您只想要一个plist文件来保存数据,您实际上不必创建和保存任何文件。有一种机制叫做NSUserDefaults. 你做类似的事情

[[NSUserDefaults standardUserDefaults] setInteger:1234 forKey:@"foo"];

and you read as in

你读到

NSInteger foo=[[NSUserDefaults standardUserDefaults] integerForKey:@"foo"];
// now foo is 1234

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 referenceand the official document.

阅读官方参考官方文档