加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 访问plist数据

发布时间:2020-12-14 19:45:41 所属栏目:百科 来源:网络整理
导读:我有一个像这样的plist: dict key11231654/key array stringhello/string stringgoodbye/string /array key78978976/key array stringok/string stringcancel/string /array 我用这个加载了plist: NSString *path = [[NSBundle mainBundle] pathForResourc
我有一个像这样的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中搜索匹配的键,而不是搜索其子项.我怎样才能做到这一点?有什么建议吗?

请提前!

解决方法

数组已编入索引.如果要访问NSArray中的第一个对象,则执行以下操作:

id someObject = [array objectAtIndex:0];

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

NSString *myString = [array objectAtIndex:0];

编辑:您需要使用NSDictionary来获取您拥有的数据 – 请注意它以< dict>开头.标签,而不是< array> tag(尽管有数组作为值).

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"];

现在,您可以使用阵列执行任何操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读