访问plist数据
我有一个像这样的 plist:
<dict>
<key>11231654</key>
<array>
<string>hello</string>
<string>goodbye</string>
</array>
<key>78978976</key>
<array>
<string>ok</string>
<string>cancel</string>
</array>
我已经使用以下方法加载了 plist:
NSString *path = [[NSBundle mainBundle] pathForResource:
@"data" ofType:@"plist"];
// Build the array from the plist
NSMutableArray *array3 = [[NSMutableArray alloc] initWithContentsOfFile:path];
如何访问 plist 的每个元素? 我想在 plist 中搜索匹配的键,然后搜索它的子键。我该怎么做?有什么建议吗?
提前致谢!
i have a plist which goes like this:
<dict>
<key>11231654</key>
<array>
<string>hello</string>
<string>goodbye</string>
</array>
<key>78978976</key>
<array>
<string>ok</string>
<string>cancel</string>
</array>
i've load the plist using this:
NSString *path = [[NSBundle mainBundle] pathForResource:
@"data" ofType:@"plist"];
// Build the array from the plist
NSMutableArray *array3 = [[NSMutableArray alloc] initWithContentsOfFile:path];
how can i access each element of my plist?
i want to search for a matching key in the plist and than search with its child. how can i do this? any suggestion?
thks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数组是有索引的。如果你想访问 NSArray 中的第一个对象,你可以执行以下操作:
如果你知道对象类型,你可以这样做:
编辑:你需要对您拥有的数据使用
NSDictionary
- 请注意,它以
标记开头,而不是
> 标签(尽管有数组作为值)。现在您可以对阵列做任何您想做的事情。
Arrays are indexed. If you wanted to access the first object in an
NSArray
, you’d do the following:If you know the object type, you could do it like this:
EDIT: You need to use an
NSDictionary
for the data that you have—note that it starts with a<dict>
tag, not an<array>
tag (though there are arrays as values).Now you can do whatever you want with your array.
此处 是一个讨论的链接,该讨论提供了一个很好的示例,说明如何访问数据以及在某些情况下哪种类型的存储方案更有利。 @Jeff Kelley 的解决方案是目标,我只是认为这个问题会添加额外的资源。希望这有帮助!
Here is a link to a discussion that provided a very good example of how to access your data and which type of storage schemes would be more beneficial under certain circumstances. @Jeff Kelley's solution is on target, I just thought this question would add an additional resource. Hope this helps!
这就是您需要创建 plist
将 root 设置为 array 而不是dictionary 的方式,这将按顺序给出结果。
使用下面的代码行,您可以获取完整的 plist。
之后,您可以使用以下代码获取任何特定内容:
使用此代码,您可以从 plist 获取 integer 、 string 、 boolean 。!
This is how you need to create plist
Set root to array instead of dictionary , this will give you results in sequence.
With below lines of code you can fetch complete plist.
After that you can fetch any specific content with below code:
With this code you can fetch integer , string , boolean from plist.!