
Hello, i’m trying to take detailed information from the database, i create a simple database, and i display the Grocery attribute fruit in a table view for example:
Apple
Grapes
Strawberry
Melon
…
if i click on the apple row for example,i want retrieve the details information for that object: so i want see only the the fruitName grouped for fruitID, for example there are a lot of apple fruit type, and every type have a id…so i want see only the apple Type for a ID, i hope i’m explain well the problem.
i found only this method:
- (NSArray *)sortFruit {
NSSortDescriptor *sortLastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"fruitID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortLastNameDescriptor, nil];
return [[[fruit valueForKey:@"detailes"] allObjects] sortedArrayUsingDescriptors:sortDescriptors];
}
but in this way i can only take all detail for that fruit, and sorted by fruitID, but i can’t choose a value…so i can’t see only a specific fruitID group…
EDIT: Thank you very much for the answer, ok now in this way:
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"%K == %@",
@"fruitID", [NSNumber numberWithInt:0]];
NSArray *arrayfiletered = [[self sortEpisode] filteredArrayUsingPredicate:predicate];
i can retrieve the info for a specific fruitID, but there is a way to see how many different fruitID there are for that fruit?
The answer you want is in the NSPredicate class. See specifically the function filteredArrayWithPredicate: in the NSArray class. It will return a new array based on the predicate that you pass in. This is an article about how to use predicates.