获取plist的内容

发布于 2024-11-27 15:16:03 字数 244 浏览 0 评论 0原文

我怎样才能获取这个plist的内容?

我对不同的元素以及如何访问它们有点困惑。

在此处输入图像描述

如何访问这些词典以及词典中的词典?

如果我想先复制这些数据,而不是总是从 plist 中读取,它会大大提高性能吗?

如何通过遍历结构级别将所有这些数据放入我的应用程序中?

谢谢

How can I get the content of this plist?

I am a little confused on the different elements and how to access them.

enter image description here

How does one access these dictionaries, and dictionaries in dictionaries?

What if I want to copy this data first, would it increase performance by a lot, rather than always reading from the plist?

How can I get all of this data into my app, by going through the structure levels?

Thank you

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

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

发布评论

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

评论(4

尴尬癌患者 2024-12-04 15:16:03

读取属性列表的最简单方法是使用 NSDictionary 上的便捷方法(如果根元素是数组,则使用 NSArray,如下所示:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"FileName" 
                                                     ofType:@"plist"];
NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:filePath];

从那时起,plist 文件就只是一个普通的字典。使用 objectForKey: 等获取值。为了更快地访问深层子节点,您可以使用 valueForKeyPath: ,如下所示:

NSString* name = [plist valueForKeyPath:@"mainDictionary.name"];

对于 plist 文件上更复杂的操作,您应该使用 NSPropertyListSerialization ,它具有多个类方法以获取更多信息对读取和写入属性列表文件的细粒度访问。

The easiest way to read a property list is by using a convenience method on NSDictionary (Or NSArray if your root element is an array) like so:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"FileName" 
                                                     ofType:@"plist"];
NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:filePath];

From there on the plist file is just a normal dictionary. Use objectForKey:, etc to get to the values. For faster access to deep child nodes you can use valueForKeyPath: like this for example:

NSString* name = [plist valueForKeyPath:@"mainDictionary.name"];

For more complex operations on plist files you should use NSPropertyListSerialization that have several class methods for more more fine grained access to reading and writing property list files.

停滞 2024-12-04 15:16:03
    use this :
    NSDictionary *dic1 = [[NSDictionary alloc] initWithContentofFile:yourFilePathhere];

// return main dictionary
    NSDictionary *dic2 = [dic1 objectForKey:mainDictionary];
    use this :
    NSDictionary *dic1 = [[NSDictionary alloc] initWithContentofFile:yourFilePathhere];

// return main dictionary
    NSDictionary *dic2 = [dic1 objectForKey:mainDictionary];
野心澎湃 2024-12-04 15:16:03

您应该查看 NSPropertyListSerialization class,这使得解析 plist 相对简单。

这是一些示例代码: http://iphoneincubator.com/blog/tag/nspropertylistserialization

You should check out the NSPropertyListSerialization class, which makes parsing plists relatively straightforward.

Here's a bit of sample code: http://iphoneincubator.com/blog/tag/nspropertylistserialization

夜夜流光相皎洁 2024-12-04 15:16:03

SWIFT代码

let filePath:String = NSBundle.mainBundle().pathForResource("infoList_English", ofType: "plist")!
let postDictionary : NSDictionary = NSDictionary(contentsOfFile: filePath)!
print(postDictionary)

Swift Code

let filePath:String = NSBundle.mainBundle().pathForResource("infoList_English", ofType: "plist")!
let postDictionary : NSDictionary = NSDictionary(contentsOfFile: filePath)!
print(postDictionary)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文