NSPredicateEditor in Xcode 4
Asked Answered
L

4

7

Bit of an issue with Xcode 4's predicate editor controls - I think I'm doing everything right, and it really seems like the IDE itself is busted.

I'm having this issue in an app I'm writing, but to isolate it, I did the following:

Create a new project with a window. In the XIB editor, add an NSPredicateEditor to it, and add one line. Leave it as key paths/strings and add two key paths - "title" and "writer". Make it case and diacritical insensitive.

Create a subclass of NSWindowController and add an IBOutlet for the predicate editor.

In awakeFromNib, put the following code:

NSPredicate *myPredicate = [NSPredicate predicateWithFormat: @"(title CONTAINS[CD] %@) AND (writer CONTAINS[CD] %@)", @"", @""];

[_predicateEditor setObjectValue:myPredicate];

This yields the following in the console:

2011-04-12 15:59:37.709 PredicateTest[38419:903] Warning - unable to find template matching predicate title CONTAINS[cd] ""

2011-04-12 15:59:37.710 PredicateTest[38419:903] Warning - unable to find template matching predicate writer CONTAINS[cd] ""

When I click the (+) button to add a new row, I get the following:

2011-04-12 15:59:40.044 PredicateTest[38419:903] Cannot create a comparison predicate with nil operator or expression.

Am I right in thinking I'm not doing anything wrong here, and this should work? If I change the predicate editor row template away from key paths, then switch it back to key paths, and edit the list of key paths, Xcode crashes with an internal consistency exception, which makes me think maybe Xcode 4 is not up to snuff when it comes to predicate editing.

Anyone got any ideas? I've tried creating an NSPredicateEditor in code rather than the XIB editor, and endless messing around to no avail. The predicate editing functionality is all that's holding me up releasing an app to the store, so this is kind of an annoyance.

Lowercase answered 12/4, 2011 at 23:10 Comment(1)
Laboriously went to the trouble of downloading XCode3 and IB, and when I open the XIB, the key paths are missing. Can XCode4 not properly save keypaths in NSPredicateEditorRowTemplates?Lowercase
F
13

I know this isn't the answer you want to hear, but I highly recommend setting up the the predicate editor programmatically. Setting it up in IB, in my experience, isn't very intuitive. At least in code you can explicitly see what's going on.

NSArray *keyPaths = @[[NSExpression expressionForKeyPath:@"title"],
                      [NSExpression expressionForKeyPath:@"writer"]];
NSArray *operators = @[@(NSEqualToPredicateOperatorType),
                       @(NSNotEqualToPredicateOperatorType),
                       @(NSBeginsWithPredicateOperatorType),
                       @(NSEndsWithPredicateOperatorType),
                       @(NSContainsPredicateOperatorType)];

NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions:keyPaths
                                                                          rightExpressionAttributeType:NSStringAttributeType
                                                                                              modifier:NSDirectPredicateModifier 
                                                                                             operators:operators 
                                                                                               options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];

NSArray *compoundTypes = @[@(NSNotPredicateType),
                           @(NSAndPredicateType),
                           @(NSOrPredicateType)];
NSPredicateEditorRowTemplate *compound = [[NSPredicateEditorRowTemplate alloc] initWithCompoundTypes:compoundTypes];

[myPredicateEditor setRowTemplates:@[template, compound]];
Franctireur answered 13/4, 2011 at 3:5 Comment(1)
No problem - this actually turned out to be a rock solid solution, although you're right, it wasn't what I wanted to hear. So thanks!Lowercase
J
3

It seems to be a bug in xCode 4 indeed. When you log the predicate formed by the predicateEditor, you'll see that it sounds

"date" >= CAST(344464706.878616, "NSDate")

instead of:

date >= CAST(344464706.878616, "NSDate")

The first style occurs in xCode 4 and the latter in xCode 3.

Jim answered 5/12, 2011 at 20:45 Comment(1)
aha, so it looks like the "date" bit is getting encoded as a constant value instead of as a key path?Franctireur
L
1

I have the same issue with XCode 4 and NSPredicateEditor, so it's not just you. I was able to open my code in XCode 3, delete the bindings in IB and reassign the bindings and it worked normally. It has something to do with the way that XCode 4 is setting up the bindings – it appears to be buggy. Setting it programmatically is probably the best answer if XCode 3 is not an option.

Longbow answered 18/4, 2011 at 0:37 Comment(0)
B
0

running into the same problem with XCode4, unfortunatly i can't test no more with Xcode3's IB.

Starting with the sample found here http://nvie.com/posts/nspredicateeditor-tutorial/ i suspect XCode4 IB to have bugs, because adding a keypath in Predicate Editor via IB and adding an entry in the DEFAULT_PREDICATE leads to "Warning - unable to find template matching predicate".. Check this sample to understand ;) Seems there's known issue ? Maybe i'm missing something..

Bonnet answered 15/4, 2011 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.