访问plist数据

发布于 2024-09-14 12:26:56 字数 740 浏览 3 评论 0原文

我有一个像这样的 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 技术交流群。

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

发布评论

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

评论(3

瑶笙 2024-09-21 12:26:56

数组是有索引的。如果你想访问 NSArray 中的第一个对象,你可以执行以下操作:

id someObject = [array objectAtIndex:0];

如果你知道对象类型,你可以这样做:

NSString *myString = [array objectAtIndex:0];

编辑:你需要对您拥有的数据使用 NSDictionary - 请注意,它以 标记开头,而不是 > 标签(尽管有数组作为值)。

NSString *path = [[NSBundle mainBundle] pathForResource:
                @"data" ofType:@"plist"];
// Build the array from the plist  
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSArray *value = [dict valueForKey:@"11231654"];

现在您可以对阵列做任何您想做的事情。

Arrays are indexed. If you wanted to access the first object in an NSArray, you’d do the following:

id someObject = [array objectAtIndex:0];

If you know the object type, you could do it like this:

NSString *myString = [array objectAtIndex:0];

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).

NSString *path = [[NSBundle mainBundle] pathForResource:
                @"data" ofType:@"plist"];
// Build the array from the plist  
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSArray *value = [dict valueForKey:@"11231654"];

Now you can do whatever you want with your array.

海未深 2024-09-21 12:26:56

此处 是一个讨论的链接,该讨论提供了一个很好的示例,说明如何访问数据以及在某些情况下哪种类型的存储方案更有利。 @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!

檐上三寸雪 2024-09-21 12:26:56

这就是您需要创建 plist

plist snapshot

将 root 设置为 array 而不是dictionary 的方式,这将按顺序给出结果。
使用下面的代码行,您可以获取完整的 plist。

NSString *pathOfPlist = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
arrPlistValue = [[NSArray alloc]initWithContentsOfFile:path];
NSLog(@"Plist Value : %@",arrPlistValue);

之后,您可以使用以下代码获取任何特定内容:

NSString *strName = [[arrTempValue objectAtIndex:0] objectForKey:@"name"];
NSString *strImage = [[arrTempValue objectAtIndex:0] objectForKey:@"image"];
int idValue = [[[arrTempValue objectAtIndex:0] objectForKey:@"id"] integerValue];
BOOL isSubCat = [[[arrTempValue objectAtIndex:0] objectForKey:@"isSubCategory"] boolValue];

使用此代码,您可以从 plist 获取 integer 、 string 、 boolean 。!

This is how you need to create plist

plist screenshot

Set root to array instead of dictionary , this will give you results in sequence.
With below lines of code you can fetch complete plist.

NSString *pathOfPlist = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
arrPlistValue = [[NSArray alloc]initWithContentsOfFile:path];
NSLog(@"Plist Value : %@",arrPlistValue);

After that you can fetch any specific content with below code:

NSString *strName = [[arrTempValue objectAtIndex:0] objectForKey:@"name"];
NSString *strImage = [[arrTempValue objectAtIndex:0] objectForKey:@"image"];
int idValue = [[[arrTempValue objectAtIndex:0] objectForKey:@"id"] integerValue];
BOOL isSubCat = [[[arrTempValue objectAtIndex:0] objectForKey:@"isSubCategory"] boolValue];

With this code you can fetch integer , string , boolean from plist.!

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