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

≯℡__Kan透↙ 提交于 2020-01-01 09:24:12

问题


I have an array which is filled with NSDictionaries. I want to find the index of one of the dictionary, but what I know about this dictionary is only a value for key @"name". How do I do it ?


回答1:


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

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

index will be NSNotFound if no matching object is found.




回答2:


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


来源:https://stackoverflow.com/questions/12404722/nsarray-full-of-nsdictionaries-how-to-find-index-of-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!