嵌套 NSArray / NSDictionary 问题

发布于 2024-09-15 04:20:02 字数 1363 浏览 9 评论 0原文

我有一个包含多个 NSDictionaries 的 NSArray 。

我有一个包含 ID 字符串的 NSString

我想做的是迭代 NSDictionaries 直到找到与 NSString 匹配的对象。然后我想返回整个 NSDictionary

我对 iPhone 开发相当陌生,所以我可能错过了一些明显的东西......提前致谢。 :)

编辑,这是保存的 .plist 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>date</key>
        <string>2011/05/20</string>
        <key>id</key>
        <string>1282007740</string>
        <key>name</key>
        <string>Test item name</string>
        <key>niceDate</key>
        <string>May 20, 2011</string>
    </dict>
    <dict>
        <key>date</key>
        <string>2010/08/15</string>
        <key>id</key>
        <string>1282075925</string>
        <key>name</key>
        <string>Test. Nothing to see here.</string>
        <key>niceDate</key>
        <string>Aug 15, 2010</string>
    </dict>
</array>
</plist>

假设我的 NSString 是“1282075925”。我想获取具有匹配的 id 键的字典。

I have an NSArray containing several NSDictionaries.

I have an NSString containing an ID string.

What I'm trying to do is iterate through the NSDictionaries until I find the object that matches the NSString. Then I want to return the entire NSDictionary.

I'm fairly new to iPhone development, so I'm probably missing something obvious... Thanks in advance. :)

Edit, here's the .plist file that gets saved out:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>date</key>
        <string>2011/05/20</string>
        <key>id</key>
        <string>1282007740</string>
        <key>name</key>
        <string>Test item name</string>
        <key>niceDate</key>
        <string>May 20, 2011</string>
    </dict>
    <dict>
        <key>date</key>
        <string>2010/08/15</string>
        <key>id</key>
        <string>1282075925</string>
        <key>name</key>
        <string>Test. Nothing to see here.</string>
        <key>niceDate</key>
        <string>Aug 15, 2010</string>
    </dict>
</array>
</plist>

Let's say my NSString is "1282075925". I want to get the dictionary with the id key that matches.

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

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

发布评论

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

评论(2

偏爱自由 2024-09-22 04:20:02

此代码应该执行您想要的操作 - 最后,dict 将指向您正在搜索的 NSDictionary,或者它将是 nil如果没有匹配的字典。

// (These are assumed to exist)
NSArray *dictionaries;
NSString *idToFind; 



NSDictionary *dict = nil;
for (NSDictionary *aDict in dictionaries) {
    if ([[aDict objectForKey:@"id"] isEqualToString:idToFind]) {
        dict = aDict;
        break;
    }
}

This code should do what you want - at the end, dict will either point to the NSDictionary you are searching for, or it will be nil if there was no matching dictionary.

// (These are assumed to exist)
NSArray *dictionaries;
NSString *idToFind; 



NSDictionary *dict = nil;
for (NSDictionary *aDict in dictionaries) {
    if ([[aDict objectForKey:@"id"] isEqualToString:idToFind]) {
        dict = aDict;
        break;
    }
}
十年九夏 2024-09-22 04:20:02

如果您正在搜索@“12345”,键为@“id”

for (NSDictionary* dictionary in array1) {
   id value = [dictionary valueForKey:@"id"];
   if ([value isKindOfClass:[NSString class]]) {
      NSString* string = (NSString*) value;
      if ([string isEqualToString:@"12345"]) {
          return dictionary;
      }
   }
}
return nil;

If you are searching for @"12345", with a key of @"id"

for (NSDictionary* dictionary in array1) {
   id value = [dictionary valueForKey:@"id"];
   if ([value isKindOfClass:[NSString class]]) {
      NSString* string = (NSString*) value;
      if ([string isEqualToString:@"12345"]) {
          return dictionary;
      }
   }
}
return nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文