xcode NSArray 充满了 NSDictionaries。如何找到对象的索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12404722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:28:49  来源:igfitidea点击:

NSArray full of NSDictionaries. How to find index of object?

iphoneobjective-ciosxcodensarray

提问by SmartTree

I have an arraywhich is filled with NSDictionaries. I want to find the indexof one of the dictionary, but what I know about this dictionary is only a valuefor key @"name".How do I do it ?

我有一个array充满NSDictionaries. 我想找到index其中一本字典的 ,但我对这本字典的了解只是一个valuefor 键@"name".我该怎么做?

回答by Martin R

Find index of first dictionary in theArraywhose value for @"name"is theValue:

查找第一库的指标theArray,其值@"name"theValue

NSUInteger index = [theArray indexOfObjectPassingTest:
        ^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop)
        {
            return [[dict objectForKey:@"name"] isEqual:theValue];
        }
];

indexwill be NSNotFoundif no matching object is found.

indexNSNotFound如果没有匹配的对象被发现。

回答by kartik patel

 NSArray *temp = [allList valueForKey:@"Name"];
 NSInteger indexValue = [temp indexOfObject:YourText];
 NSString *name = [[allList objectAtIndex:indexValue] valueForKey:@"Name"]