如何在iphone中以编程方式创建PLIST文件 [英] How to create PLIST files programmatically in iphone

查看:149
本文介绍了如何在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);

这篇关于如何在iphone中以编程方式创建PLIST文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆