2

I can't understand how to use predicate, I have a very long code to filter objects from array by property "type" and suddenly I saw method "filteredArrayUsingPredicate" that can make my life better. I try write predicates but I always get errors; can someone show me how to write it right?

I have method - (void) filterData: (NSString *)filteredWord:

I also have array with objects (Event): NSArray *eventsArray. I want use a filteredArrayUsingPredicate to get new array with objects (Event) where their property (type) is equal filterWord. Note that Event is Core Data Managed subclass.

Is it even possible to do this with predicate?

One of my attempts:

NSString *propertyName = @"type";
NSArray *eventsArray = [[[[self currentPerson] events] objectEnumerator]allObjects];
NSPredicate *predicte = [NSPredicate predicateWithFormat:@"%k like '%@'",propertyName,filteredWord];
[eventsArray filteredArrayUsingPredicate:predicte];

1 Answer 1

2

Try this:

NSString *propertyName = @"type";
NSArray *eventsArray = [[[self currentPerson] events] allObjects];
NSPredicate *predicte = [NSPredicate predicateWithFormat:
                         @"%k like %@",propertyName, filteredWord];
NSArray *filteredArray = [eventsArray filteredArrayUsingPredicate:predicte];

You ignore the filtered result. The filteredArrayUsingPredicate: returns a new instance of NSArray. It doesn't filter the original array in place, because NSArray objects are immutable. You either have to make an NSMutableArray and then use filterUsingPredicate: to filter the array in place, or you have to do something with the returned array of filteredArrayUsingPredicate: (log it, save it etc...)

Don't use single quotes around the property. (thanks for the clue @MartinR)

Sign up to request clarification or add additional context in comments.

11 Comments

thank you for your comment I think I understand little bit about predicts.. but in some reason filteredArray has 0 objects when I am sure that there more then one objects with filtered type. by the way I use [[array objectEnumerator] allObjects] because events are NSSET.
In that case you can use [set allObjects]. No need for an enumerator. Also, the type property of UIEvent is not a string. It's an enum (integer). See my updated answer.
Actually, "%K" can also be used instead of "SELF.%@". "%K" is a var arg substitution for a key path in predicates. - And I happen to know (from Dennis' previous questions) that Event is a Core Data managed object subclass and not related to UIEvent. So "%K LIKE %@" should work.
@DrummerB same, 0 objects, the type is string and I think the first answer was better that this because in the first one I used enumerator to get array of Events, predicate as I understand filter "Event.type == "filteredWord" Events that good entered to filteredArray the problem is that not really happened. maybe something wrong with the predict or transfer to the new Array?
Martin R you right I forgot say that Event is a Core Data Managed object, I will edit my question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.