Jonathan Thurft
Jonathan Thurft

Reputation: 4173

Search in an NSArray whose content are fetched CoreData objects by an specific object atribute

I've a NSArray that contains different Exercise objects that were fetched from CoreData.

In another method in my program I need to find an object whose property matches a criteria eg: Exercise.exerciseName == @"bench press" lets say I want to get the Exercise Object with the name of bench press.

I know I could achieve by doing the below code, but I was wondering if there was a more efficient way to do it in objective-c with a predicate or something like that.

for (Exercise*exercise in _exercises) {
    if( [exercise.exerciseName isEqualToString:@"bench press"] )
    {
        // do something
    }

}

Upvotes: 1

Views: 94

Answers (1)

Michał Ciuba
Michał Ciuba

Reputation: 7944

NSArray* filteredExercises = [_exercises filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"excerciseName == %@", @"bench press"]];

Upvotes: 2

Related Questions