1
votes

I'm attempting to search an array of objects using NSPredicate. The objects have a number of NSString properties that I want to filter.

I have the following code to do the filtering:

NSString *predicateString = [NSString stringWithFormat:@"name like[c] %@",self.textFieldOutlet.text];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:predicateString];
NSArray *results = [[MySingleton sharedMySingleton].cardArray filteredArrayUsingPredicate:searchPredicate];

But I get the following error:

WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSUnknownKeyException> [<CardObject 0x14a530> valueForUndefinedKey:]: this class is not key value coding-compliant for the key test.

If I put single quotes around the %@ it works but only if the entered text is exactly the same as the value held for the name key and not if it is 'like' the value. Thus not very useful. I must be doing something wrong with the predicate string, can anyone advise?

Thanks in advance!

3
Will you post the value to be stored in predicateString?alloc_iNit
the error looks unrelated. Can you double check that the app works only when you insert single quotes?Max MacLeod
Sorry about the delay folks. @iApple predicateString = name like[c] test.bennythemink
@xj1200 I can confirm that the predicate will only return a value when I include single quotes. And it only returns something when the strings exactly match :(bennythemink

3 Answers

0
votes

Apparently the error states that your object is not key-value compliant. Try using NSDictionary instead

0
votes

The error states that your CardObjects don't support the property 'test' with KVO.

However, your predicate simply asks to compare the name property so it looks like this predicate isn't the reason that your webview is crashing.

Can you add the contents of your webView:shouldInsertText:replacingDOMRange:givenAction: method to the question because that's where the bug is occurring.

I think that changing the predicate is changing the flow of the code later in this method, and that's where the error is (but without seeing the rest of your code, that's a bit of a guess!).

0
votes

Ok I still have no idea why I get a webview error for the NSPredicate. But I have fixed the issue using the following predicate string:

(name like[cd] '*%@*')