-1

像这样的数组:

    <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可以被访问。

应该做的正确方法是什么?

4

2 回答 2

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);     
    }
}
于 2013-09-27T00:17:18.407 回答
1

您需要遍历外部数组,并且在该循环中您需要遍历内部数组,查找其值为 for 的@"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
于 2013-09-26T05:03:26.183 回答