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

发布于 2024-11-24 11:50:50 字数 1487 浏览 1 评论 0原文

我正在寻找以编程方式在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城孤痞 2024-12-01 11:50:50

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

您可以使用此代码创建、添加值以及从 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);

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.

//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);
甜味拾荒者 2024-12-01 11:50:50

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.

洛阳烟雨空心柳 2024-12-01 11:50:50

请注意,如果您只想用一个 plist 文件来保存数据,则实际上不必创建和保存任何文件。有一种称为 NSUserDefaults 的机制。您执行类似的操作

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

,然后阅读

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

准备要保存的文件,将其写入文件,在下次启动应用程序时再次读取它,自动为您完成!

阅读官方参考官方文档

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 called NSUserDefaults. You do something like

[[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 reference and the official document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文