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
NSArray full of NSDictionaries. How to find index of object?
提问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.
index会NSNotFound如果没有匹配的对象被发现。
回答by kartik patel
NSArray *temp = [allList valueForKey:@"Name"];
NSInteger indexValue = [temp indexOfObject:YourText];
NSString *name = [[allList objectAtIndex:indexValue] valueForKey:@"Name"]

