Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 616
SmartTree Avatar asked Sep 13 '12 10:09

SmartTree


2 Answers

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.

like image 197
Martin R Avatar answered Oct 22 '22 03:10

Martin R


 NSArray *temp = [allList valueForKey:@"Name"];
 NSInteger indexValue = [temp indexOfObject:YourText];
 NSString *name = [[allList objectAtIndex:indexValue] valueForKey:@"Name"]  
like image 1
kartik patel Avatar answered Oct 22 '22 03:10

kartik patel