small medium large xlarge

Back to: All Forums  Core Data
29 Apr 2009, 11:21
Christopher Corbettis (2 posts)

On page 50 we are told bq. [T]he NSSearchField interface element works off the principle of an NSPredicate […] using the Controller Key, Model Key Path and Value Transformer to bind it to our data. For example, if we wanted to filter on the name of recipes, we would bind this NSSearchField to our Recipe NSArrayController using the Controller Key of Arranged Objects and a Model Key Path of name.

I must be doing something wrong as this binding crashed the application. Digging further I tried the very handy tip of option - dragging an entity from Xcode onto an IB window, this pops up a ‘New Core Data Entity Interface’, where I had it add a search field, the field works as expected.

The binding for this search field is to the Recipe NSArrayController, but with a Controller Key of filterPredicate, no Model Key Path and a Predicate Format of (name contains[c] $value), the parenthesis seem optional from my limited testing. Adding “name” to the Model Key Path breaks the predicate.

Thanks for reading!

27 Jul 2009, 06:18
Marcus S. Zarra (284 posts)

if you set the model key path and set the predicate to @keyPath contains[c] $value@ instead with a Controller Key of filterPredicate then it will work as the predicate will replace @keyPath@ with whatever you set in the model key path.

Also, as you have discovered you can skip the model key path by setting the predicate directly.

04 Aug 2009, 02:16
Felipe Laso (104 posts)

I tried assigning the model key path to “name” and then in the predicate i put “keyPath contains[c] $value” it just doesn’t work.

I dunno if my .xib file is acting up or something but for me it only worked when I left the model key path blank and used “name contains[c] $value”.

Just thought I’d share this.

07 Aug 2009, 21:52
Michael Sherman (2 posts)

Just to corroborate Felipe’s statement; I have the same issue. “keyPath” is not being substituted by the model key path. To get it working, I cleared the model key path field and set the predicate to @name contains[c] $value@. btw I’m using XCode 3.1.1 (10.5.7)

17 Aug 2009, 20:36
Leon Starr (1 post)

Yeah, that was driving me crazy for a while - my experience and solution is same as Felipe’s and Michael’s. Thanks guys! And I am running XCode 3.1.3 (10.5.8). Now I will have to study up on predicates to figure out why it works now!

25 Aug 2009, 16:29
Marcus S. Zarra (284 posts)

I would suggest taking a look at the example path. @keyPath@ does get replaced in the example and the search works as expected. The fact that it is not working may indicate another issue as that is definitely the intended result.

11 Sep 2009, 00:48
Christopher Roach (7 posts)

Hi Marcus, I was having the same problem as everyone else here, and so I decided to take a look at the sample code that you’ve supplied for the book per your suggestion. The sample code though is also using the “name contains[c] $value” predicate with the model key path left blank. So, it looks like the solution in the book currently does not work. Is this something that is just broken in the current version of Xcode?

11 Sep 2009, 01:27
Christopher Roach (7 posts)

Hi Marcus, I just wanted to post back and say that upon further investigation, it looks like the only way this will work is if the model key path is left blank and the predicate is set to “name contains[c] $value”. If you look at the binding value for the Predicate that IB puts together when specifying the model key path it is “”. This shouldn’t work as filterPredicate is returning the NSPredicate object for the Recipes NSArrayController object and it should not have a name attribute attached it. The name attribute that we are referring to is associated with the Recipe entity and not the predicate object. So, it looks like the only way this will work is with the solution that Christopher and the others stated above. What do you think? Any further info on this issue that you can provide?

18 Oct 2009, 19:43
Marco (3 posts)

same issue here, with the same solution:

removing “name” from “model key path” and changing the predicate format to “name contains $value”.

however, running the application, the search box works only once. if i try searching for another string, i get the following message:

bq. Grokking Recipes[20879:a0f] Error setting value for key path filterPredicate of object <NSArrayController: 0x10013a410>[entity: Recipe, number of selected objects: 0] (from bound object <NSSearchField: 0x100221e10> with object ID 606 in nib named MainMenu.nib): [<NSComparisonPredicate 0x10020ee00> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.

any idea what i might be doing wrong?



24 Oct 2009, 18:21
Lucas Gladding (1 post)


I was thinking the same thing as Christopher. The controller key and model key path fields are always joined when setting the binding. Obviously we want to bind the NSSearchField predicate to “filterPredicate”, not “”.

- (void)bind:(NSString *)binding 
withKeyPath:(NSString *)keyPath 
options:(NSDictionary *)options

In any case, thanks for writing the book. I’m looking forward to the chapters about migration and sync services. :)

08 Dec 2009, 11:29
Carlton Gibson (7 posts)

Hi Marcus,

Just adding that I’ve had exactly the same problem as here. Following the example as given in the P1.0 version the search field does not work. Following the fix described above does.

Perhaps there’s something else going on to get the keyPath substitution to work?

Great book. Thanks!

Regards, Carlton

10 Dec 2009, 08:15
Marcus S. Zarra (284 posts)

I definitely need to explore this a bit more and since it is not strictly a Core Data issue probably do an article about it and then update the errata in the book as needed.

Thanks for mentioning it again, I have added it to my Things list :)

04 Jun 2010, 15:18
Cem Karan (2 posts)

I don’t have anything more to add, but I can confirm what Marco mentions in his Oct 18, 2009 post. Once I do a search, the list is properly filtered, but there isn’t any way to unfilter the list; if I search for pot roast, and then clear out the search field, I’m just left with pot roast. Restarting the application brings everything back, so nothing is getting lost, just not displayed.

25 Feb 2011, 20:44
Julia Eckel (1 post)

It’s some time since the last post, but if anyone should find this while searching for a solution - here it is:

Bind to: Recipes ControllerKey: filterPredicate Predicate Format: name contains[c] $value All other fields: empty NOW UNCHECK “Raises for not applicable keys” and it should work.

You must be logged in to comment