Re: Weird NSPredicate structure when using "first" in keypath
Re: Weird NSPredicate structure when using "first" in keypath
- Subject: Re: Weird NSPredicate structure when using "first" in keypath
- From: Aaron Tuller <email@hidden>
- Date: Mon, 09 Jan 2017 18:38:48 -0800
Try doing
name.#first
as FIRST and LAST are reserved words and need escaping:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html
I just tried:
NSComparisonPredicate *p = (NSComparisonPredicate *)[NSComparisonPredicate predicateWithFormat:@"name.#first == 'Aaron'"];
NSDictionary *myself = @{@"name": @{@"first" : @"Aaron", @"last" : @"Tuller"}};
NSDictionary *yourself = @{@"name": @{@"first" : @"Jens", @"last" : @"Alfke"}};
NSLog(@"%i", [p evaluateWithObject:myself]);
NSLog(@"%i", [p evaluateWithObject:yourself]);
works as expected.
-aaron
> On Jan 9, 2017, at 6:01 PM, Jens Alfke <email@hidden> wrote:
>
> I’m writing code that walks through an NSPredicate and generates an alternate form of query from it (similar to what CoreData and Realm do.)
>
> I’m seeing some very weird undocumented behavior when format strings are compiled into NSPredicates, if the format string includes a key-path with a property named “first” or “last”. The resulting predicate contains an NSExpression of an undocumented type.
>
> - For example, “name.middle == ‘Bob’” compiles to what you’d expect; the LHS of the ‘equals’ predicate is an expression of NSKeyPathExpressionType, with key-path “name.middle”.
> - But “name.first == ‘Bob’” results in an LHS that’s of NSFunctionExpressionType, with a function selector “valueForKeyPath:”, and even weirder, the expression’s argument is an expression with the undocumented expressionType 11. I don’t know what that is supposed to mean, except that its -description is “FIRST” (all caps).
>
> NSComparisonPredicate
> predicateOperatorType = NSEqualToPredicateOperatorType
> leftExpression =
> NSKeyPathExpression
> expressionType = NSFunctionExpressionType
> function = “valueForKeyPath:”
> operand =
> NSKeyPathExpression
> expressionType = NSKeyPathExpressionType
> keyPath = “name”
> operand = SELF
> arguments = [
> NSSymbolicExpression
> expressionType = 11 (?????)
> ]
> rightExpression =
> NSExpression
> expressionType = NSConstantValueExpressionType
> constantValue = @“Bob”
>
> Is there something magic about the property name “first”? The only related thing I’ve seen is that in predicate syntax arrays can be indexed with “[FIRST]” or “[LAST]”, but that’s in an array subscript not a keypath…
>
> —Jens
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden