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

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

+6
source share
2 answers

Find the index of the first dictionary in theArray whose value for @"name" is equal to 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.

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

Source: https://habr.com/ru/post/925303/


All Articles