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

Ios 如何在数组中获取字典中的值索引,ios,objective-c,indexing,nsarray,nsdictionary,Ios,Objective C,Indexing,Nsarray,Nsdictionary,这样的数组: 放置 A. 名称 B 对象 C 放置 D 名称 E 对象 F 放置 X 名称 Y 对象 Z 放置 s 名称 T 对象 U ... //出于某种原因,我通过删除原始顶级数组简化了此示例的级别。现在它们看起来像是由一些字典组成的数组 例如,可以通过[[objectArray objectAtIndex:i]valueForKey:@“Place”]获取任何值,如果类似于X 然后,在另一个仍然位于相同.m的位置,只需利用X,即可反向获得X所在字典的索引。因为在这里,只能访问objec

这样的数组:


放置
A.
名称
B
对象
C
放置
D
名称
E
对象
F
放置
X
名称
Y
对象
Z
放置
s
名称
T
对象
U
...
//出于某种原因,我通过删除原始顶级数组简化了此示例的级别。现在它们看起来像是由一些字典组成的数组

例如,可以通过
[[objectArray objectAtIndex:i]valueForKey:@“Place”]
获取任何值,如果类似于X

然后,在另一个仍然位于相同
.m
的位置,只需利用X,即可反向获得X所在字典的索引。因为在这里,只能访问objectArray


正确的方法应该做什么?

您需要在外部数组上循环,在该循环中,您需要在内部数组上循环,查找
@“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
再次使用KVC

有数组时有3行:D

短的 完整示例
#导入
int main(int argc,char*argv[]){
@自动释放池{
//构建演示阵列
NSDictionary*d1=@{@“地点”:@“clackson”};
NSDictionary*d2={“地点”:“柏林”};
NSDictionary*d3=@{@“地点”:@“科隆”};
NSDictionary*d4=@{@“地点”:@“mclean”};
NSArray*数组的宽度=@[d1、d2、d3、d4];
//各就各位
NSArray*arrayWithPlaces=[arrayWithDicts valueForKey:@“Place”];
断言(arrayWithDicts.count==arrayWithPlaces.count);
//找个地方,找个地方
NSInteger索引=[arrayWithPlaces indexOfObject:@“科隆”];
NSDictionary*dict=index!=NSNotFound?数组与dicts[index]:nil;
NSLog(@“%@”,dict);
}
}

您的数组有两层,但您只显示了一层索引。首先,感谢您的详细代码似乎可以解决我的问题。但是……它看起来非常简洁,对我来说也很复杂,尤其是那些三元运算符。如果我能理解你的代码,我不会提出这个问题。。。由于我是应用程序开发新手……:)我将示例代码简化为由一些字典组成的一级数组。其他条件保持不变。期待着以一种新手可以理解的正常方式更新代码,就像我一样。提前谢谢@IO.DEV:请求更简单的代码,而不是通过这不是非常复杂的代码自己工作,将无法帮助下一个层次。@ IO.DEV,然后考虑我的基于KVC的答案。它的代码更少;)@是的,我完全迟到了,但是你应该接受Daij Djan的答案(不是这个)。很好,回答得很好!不知道您可以通过这种方式访问值。谢谢
@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
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);     
    }
}