I’m sure this question has been asked but I don’t know what to search on.
I have array of Message objects with the following fields {selected[BOOL], messageText[STR]}. I want to filter this array to get only the objects with selected=TRUE. So far so good.
NSArray *messagesFiltered = [self.fetchedResultsController.fetchedObjects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"selected == TRUE"]];
However, I don’t need the objects themselves in the returned array, I need an array of the messageText strings. How to modify predicate to return only the messageText strings and not the whole object?
That’s not really the job of a predicate. Predicates are only for filtering, not modifying, the array you apply them to.
However, there’s a fun little trick: the method
-valueForKey:will apply the kind of transform you want to your array. Just call it with themessageTextkey that you want:On an array,
-valueForKey:asks each element for a value for the given key, then returns an array of everything the elements returned. (Any element that returnsnilfor the key you pass shows up as[NSNull null]in the resulting array.)