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

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

问题描述:

我正在寻找在我的应用程序文档文件夹中创建plist文件在目标C.我在文档目录中创建一个文件夹:

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

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/Data.plist", documentsDirectoryPath];

我试图创建一个看起来像XML文件的plist文件。
/ ****必需的XML文件**** /

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>

所需的文件需要一个数组,我如何改变这个?
我也知道如何写文件到路径,但主要的问题是如何创建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文件(也称为属性列表文件)使用XML格式存储对象,如数组,字典和字符串。

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

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

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

//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 reterive the data from the plist
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *value = [savedValue objectForKey:@"value"];
NSLog(@"%@",value);