Re: Creating an NSExpression with a keypath and variable
Re: Creating an NSExpression with a keypath and variable
- Subject: Re: Creating an NSExpression with a keypath and variable
- From: Dave DeLong <email@hidden>
- Date: Wed, 16 Mar 2011 11:28:06 -0700
Directly, yes. NSExpression stores a keypath as a single string, whereas variables are store in their own kind of NSExpression object. When you replace variables with new values, it's only looking for the certain kinds of NSExpression objects to replace. Everything else stays the same.
Some notes about your predicate:
- I'm not sure you can do "$x isKindOfClass: %@" as the predicate to a subquery, although I haven't tried it. If it doesn't work, you could do this with a FUNCTION ("FUNCTION($x, 'ks_isKindOfClass:', %@)"). Note, however, that you'd have to create a category on NSObject that simply boxes the return value of isKindOfClass into an NSNumber. FUNCTION()'s don't like methods that return primitives.
- If you want to use a keypath with a variable, you could do:
FUNCTION($x, 'text') CONTAINS[cd] $searchString
That preserves $x as a variable expression so that it will be replaced properly.
This would make your entire predicate:
SUBQUERY(events, $x, FUNCTION($x, 'ks_isKindOfClass:', %@) AND (FUNCTION($x, 'text') CONTAINS[cd] $searchString)).@count != 0
Dave
On Mar 16, 2011, at 11:20 AM, Kyle Sluder wrote:
> Hi all,
>
> Is it really impossible to create an NSExpression of the form
> "$variableName.someKeyPath"? I can create an NSExpression for
> $variableName, and I can create an NSExpression for someKeyPath, but I
> can't create one that contains both.
>
> The actual problem is that I'm trying to create an NSPredicate for
> "SUBQUERY(events, $x, $x isKindOfClass: %@ AND $x.text CONTAINS[cd]
> $searchString).@count != 0". Since the method in question is called
> periodically (it's the action method of a filter control), I was
> hoping to create a static variable-based NSPredicate, rather than
> reconstruct the predicate every time this method gets called. But that
> didn't work because it didn't descend into the subquery's subpredicate
> to replace $searchString.
>
> --Kyle Sluder
> _______________________________________________
>
> 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