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];
Dennis
  • 704
  • 1
  • 9
  • 25

1 Answers1

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)

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • 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. – Dennis Dec 16 '12 at 15:43
  • 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. – DrummerB Dec 16 '12 at 16:27
  • 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. – Martin R Dec 16 '12 at 17:56
  • @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? – Dennis Dec 16 '12 at 18:03
  • Martin R you right I forgot say that Event is a Core Data Managed object, I will edit my question. – Dennis Dec 16 '12 at 18:12
  • Add an `NSLog` of your predicate format, the original and resulting arrays to your question. `NSLog(@"your predicate format", propertyName, filteredWord);` and `NSLog(@"Original:\n%@\nResult:\n%@", eventsArray, filteredArray);`. – DrummerB Dec 16 '12 at 18:12
  • @DrummerB ok, I got as expected SELF.type LIKE 'Wedding' (when I use @ not K). the second line I get some parameters=nil (I don't work with them yet) and type = Wedding in some objects.. (my filteredWord). when I use: NSString *propertyName = @"type"; NSArray *eventsArray = [[[[self currentPerson] events] objectEnumerator]allObjects]; NSPredicate *predicte = [NSPredicate predicateWithFormat: @"SELF.%K LIKE '%@'",propertyName,filteredWord]; NSArray *filteredArray = [[NSArray alloc]initWithArray:[eventsArray filteredArrayUsingPredicate:predicte]]; – Dennis Dec 16 '12 at 18:26
  • @Dennis: Don't enclose `%@` in quotation marks in the predicate format string! – Martin R Dec 16 '12 at 18:37
  • Martin you are a king, DrummerB thank you very very much for your help here :) Just to understand what the different between %@ and '%@'? by the way I use %K but %@ works too. – Dennis Dec 16 '12 at 18:41
  • @Dennis: Format specifiers in quotation marks are not replaced, so `LIKE '%@'` searches for the (verbatim) string "'%@'". – Martin R Dec 16 '12 at 18:47
  • ok, thanks. @DrummerB I mark your answer as right, If you can back your last answer with this little changes for other users that have this problem. thank you.. – Dennis Dec 16 '12 at 18:49