如何在数组中的字典中获取值的索引

时间:2013-09-25 23:30:16

标签: ios objective-c indexing nsarray nsdictionary

这样的数组:

    <array>
      <dict>
        <key>Place</key>
        <string>A</string>
        <key>Name</key>
        <string>B</string>
        <key>Object</key>
        <string>C</string>
      </dict>
      <dict>
        <key>Place</key>
        <string>D</string>
        <key>Name</key>
        <string>E</string>
        <key>Object</key>
        <string>F</string>
      </dict>
      <dict>
        <key>Place</key>
        <string>X</string>
        <key>Name</key>
        <string>Y</string>
        <key>Object</key>
        <string>Z</string>
      </dict>
      <dict>
        <key>Place</key>
        <string>S</string>
        <key>Name</key>
        <string>T</string>
        <key>Object</key>
        <string>U</string>
      </dict>
      ...
    </array>

//由于某种原因,我通过删除原始顶级数组来简化此示例的级别。现在它们看起来像是由一些词典组成的数组。

例如,

可以通过[[objectArray objectAtIndex:i] valueForKey:@"Place"]得到任何值,如果是X,

然后,在另一个仍在同一.m的地方,需要利用X来反向获取X所在字典的索引。因为在这个地方,只能访问objectArray。

应该做什么方法?

2 个答案:

答案 0 :(得分:8)

再次使用KVC!

当你有阵列时,

3行:D

NSArray *arrayWithDicts = @[d1,d2,d3,d4];//your array!

NSArray *arrayWithPlaces = [arrayWithDicts valueForKey:@"Place"];
NSUInteger index = [arrayWithPlaces indexOfObject:@"cologne"];
NSDictionary *dict = index != NSNotFound ? arrayWithDicts[index] : nil;

完整示例

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
        //build demo array
        NSDictionary *d1 = @{@"Place": @"clackson"};
        NSDictionary *d2 = @{@"Place": @"berlin"};
        NSDictionary *d3 = @{@"Place": @"cologne"};
        NSDictionary *d4 = @{@"Place": @"mclean"};
        NSArray *arrayWithDicts = @[d1,d2,d3,d4];

        //get all places
        NSArray *arrayWithPlaces = [arrayWithDicts valueForKey:@"Place"];
        assert(arrayWithDicts.count == arrayWithPlaces.count);

        //find place and get dict
        NSUInteger index = [arrayWithPlaces indexOfObject:@"cologne"];
        NSDictionary *dict = index != NSNotFound ? arrayWithDicts[index] : nil;

        NSLog(@"%@", dict);     
    }
}

答案 1 :(得分:1)

您需要遍历外部数组,并且在该循环中,您需要遍历内部数组,查找@"Place"的值为X的字典。这是一种方法:

@implementation NSArray (StackOverflow)

- (NSIndexPath *)indexPathOfDictionaryWithPlace:(NSString *)place {
    // I can't refer directly to an array inside a block, so I have to add an extra variable.
    NSUInteger indexStorage[2];
    NSUInteger *indexes = indexStorage;

    indexes[0] = [self indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        NSArray *subarray = obj;
        indexes[1] = [subarray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
            NSDictionary *dictionary = obj;
            return [place isEqualToString:dictionary[@"Place"]];
        }];
        return indexes[1] != NSNotFound;
    }];

    if (indexes[0] == NSNotFound) {
        return nil;
    } else {
        return [NSIndexPath indexPathWithIndexes:indexes length:2];
    }
}

@end
相关问题