xcode 访问 plist 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3494033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:38:30  来源:igfitidea点击:

access plist data

objective-ciphonexcodeplist

提问by Stefan

i have a plist which goes like this:

我有一个 plist 是这样的:

 <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:

我已经使用这个加载了 plist:

    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?

我如何访问我的 plist 的每个元素?我想在 plist 中搜索匹配的键,而不是用它的子项搜索。我怎样才能做到这一点?有什么建议吗?

thks in advance!

提前谢谢!

回答by Jeff Kelley

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

数组被索引。如果要访问 中的第一个对象NSArray,请执行以下操作:

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 NSDictionaryfor the data that you have—note that it starts with a <dict>tag, not an <array>tag (though there are arrays as values).

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

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.

现在你可以对你的数组做任何你想做的事情。

回答by Mark7777G

Hereis 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!

是一个讨论的链接,该讨论提供了一个很好的示例,说明了如何访问您的数据以及在某些情况下哪种类型的存储方案更有利。@Jeff Kelley 的解决方案是有针对性的,我只是认为这个问题会增加额外的资源。希望这可以帮助!

回答by Hemant Singh Rathore

This is how you need to create plist

这就是你需要如何创建 plist

plist screenshot

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.

将 root 设置为 array 而不是 dictionary ,这将按顺序为您提供结果。使用以下代码行,您可以获取完整的 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.!

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