将数组中的字符串写入 plist

发布于 2024-12-02 19:28:09 字数 769 浏览 0 评论 0 原文

我有一个 plist,其中包含一个数组和数组中的一些字符串。所以我想知道如何将新字符串写入 plist 中的数组。当我使用下面的代码从 plist 中获取字符串时,我只是在日志中得到 null 。

NSMutableArray *myPrimaryinfo = [[NSMutableArray alloc] initWithContentsOfFile:@"/private/var/mobile/Library/Keyboard/UserDictionaryWordKeyPairs.plist"];

NSLog(@"%@", myPrimaryinfo);

我的列表如下所示:

<?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>
    <string>Test string1</string>
    <string>Test string2</string>
    <string>Test string3</string>
    <string>Test string4</string>
</array>
</plist>

I have a plist with an array and some strings in the array. So I am wondering how to write a new string to the array in the plist. When I use the code below to get the strings from the plist I just get null in the log.

NSMutableArray *myPrimaryinfo = [[NSMutableArray alloc] initWithContentsOfFile:@"/private/var/mobile/Library/Keyboard/UserDictionaryWordKeyPairs.plist"];

NSLog(@"%@", myPrimaryinfo);

My plist looks like this:

<?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>
    <string>Test string1</string>
    <string>Test string2</string>
    <string>Test string3</string>
    <string>Test string4</string>
</array>
</plist>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

习惯那些不曾习惯的习惯 2024-12-09 19:28:09

获取如下文件的路径并尝试..

NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkArray" ofType:@"plist"];

get your path of the file like below and try..

NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkArray" ofType:@"plist"];
隔纱相望 2024-12-09 19:28:09

您无法在 iOS 上执行此操作,因为文件系统在应用程序的沙箱之外是不透明的。有关 iOS 文件系统中应用程序可用内容的更多信息,请阅读以下内容:

http://developer.apple.com/library/iOS/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/RuntimeEnvironment/RuntimeEnvironment.html#//apple_ref/doc/uid/TP40007072-CH2-SW12

不要尝试访问您的应用程序不可用的文件系统部分,而是考虑将 plist 文件包含在您的应用程序包中。如果您希望应用程序能够动态修改 plist,您可以将其副本保存到与应用程序包并行的 Documents 目录中,然后从那里读取 plist。

You can't do that on iOS, because the file system is opaque outside of your app's sandbox. For more info on what's available to your app in the iOS filesystem, read this:

http://developer.apple.com/library/iOS/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/RuntimeEnvironment/RuntimeEnvironment.html#//apple_ref/doc/uid/TP40007072-CH2-SW12

Instead of trying to access portions of the filesystem that are unavailable to your app, consider including the plist file in your app's bundle. If you want the app to be able to dynamically modify the plist, you can save a copy of it to the Documents directory parallel to the app bundle, and from then on read the plist from there instead.

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