Re: Determine keys used in NSPredicate
Re: Determine keys used in NSPredicate
- Subject: Re: Determine keys used in NSPredicate
- From: Ken Thomases <email@hidden>
- Date: Sun, 20 Oct 2013 01:08:54 -0500
On Oct 20, 2013, at 12:27 AM, Trygve Inda wrote:
> My question is: After I have an NSPredicate built by the user with the
> Predicate editor, how can determine the keys that the NSPredicate uses?
You need to traverse the hierarchy of objects represented by the predicate.
The NSPredicate you have is actually an NSCompoundPredicate or an NSComparisonPredicate. Use -isKindOfClass: to figure out which. For the former, iterate over its -subpredicates and recursively apply this algorithm to each. For the latter, examine the -leftExpression and -rightExpression.
For each expression, query its -expressionType. If it's NSKeyPathExpressionType, obtain its -keyPath, parse it into its individual keys, and compare against the key of interest. For the other types, you need to figure out which properties to query to get the appropriate sub-expressions and even sub-predicates to recurse further. The documentation doesn't lay this all out quite as explicitly as one might like, but you can figure it out by examining the convenience constructors. If in doubt, some trial and error should make it clear – querying properties which are not appropriate for a given type raises an exception.
Regards,
Ken
_______________________________________________
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