NoviceDeveloper
NoviceDeveloper

Reputation: 225

how to select Specific objects from array

I have create an array containing person firstname,lastname and number which i populate in tableview. Now i want to show only those person data whose lastname is equal to "Ahmed" This might be a simple question but i am not able to do it.Any help will be appreciated. Here is my code.

- (void)viewDidLoad {
    [super viewDidLoad];
    productArray=[[NSMutableArray alloc]init];

    PersonDetail *personObj = [[PersonDetail alloc] init];
    personObj.firstName = @"Adeem";
    personObj.lastName = @"Basraa";
    personObj.phoneNumber = @"123456789";

    [productArray addObject:personObj];
    [personObj release];

    PersonDetail *personObj = [[PersonDetail alloc] init];
    personObj.firstName = @"Ijaz";
    personObj.lastName = @"Ahmed";
    personObj.phoneNumber = @"987654321";

    [productArray addObject:personObj];
    [personObj release];

    PersonDetail *personObj = [[PersonDetail alloc] init];
    personObj.firstName = @"Waqas";
    personObj.lastName = @"Ahmad";
    personObj.phoneNumber = @"45656789";
    [productArray addObject:personObj];
    [personObj release];
}

Upvotes: 0

Views: 1783

Answers (4)

Rajive Jain
Rajive Jain

Reputation: 758

In case anyone needs this, here's an example - hope it helps.

Assume you have an array allMovies & you are searching it for a particular movie someMovie and would like to get its index in the array. allMovies is an array of movie dictionaries as an example from a JSON service.

NSUInteger theIndex = [self.allMovies indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  if ([[[obj objectForKey:@"movie"] objectForKey:@"id"] integerValue] == someMovie.id)
                *stop = YES;
                NSLog(@"IDX: %d", idx);
                return *stop;
}];
NSLog(@"FOUND INDEX FOR SEARCHED OBJECT: %d", theIndex);

Upvotes: 0

calimarkus
calimarkus

Reputation: 9977

You could use - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate on your array to retrieve a new filtered array.

To create a predicate use: (NSPredicate *)predicateWithFormat:(NSString *)format, ....

E.g.:

NSString* searchName = @"Ahmad";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lastName == '%@'", searchName];
NSArray* filteredPersons = [productArray filteredArrayUsingPredicate:predicate];

Upvotes: 4

Pavan Kumar
Pavan Kumar

Reputation: 258

use predicate to get particular objects from array like this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lastName == '%@'", nameObject];
[productArray filterUsingPredicate:predicate];

it will remove the object other than having last name as nameObj

NSMutableArray *list =   [productArray filteredArrayUsingPredicate:predicate];

this statement will gives an array having all personDetail objects having lastName as nameObj

Upvotes: 2

James Bedford
James Bedford

Reputation: 28962

You could use the following method from the NSArray superclass:

- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

This method takes a block as its argument, which is basically like a mini-function that will be called for every element of the array. Within your block code you could cast the id obj as a PersonDetail instance and then check the name is equal to "Ahmed". Once you find the PersonDetail you're looking for, you can set the stop output parameter to be true by dereferencing the third argument of the block like this.. *stop = YES.

Check the documentation for this method of NSArray. You'll probably also want to check out the blocks programming guide if you're new to the blocks syntax.

Upvotes: 0

Related Questions