Skip to content

findinstances

Dave Lee edited this page Jan 22, 2018 · 3 revisions

findinstances Additional Documentation

NSPredicate syntax

Generally, NSPredicate syntax is somewhat guessable and intuitive (relative to popular programming languages). There are two exceptions:

  1. When a keypath contains a reserved keyword, it has to be escaped with #. An example of this is referring to a layer's size. Since size is a reserved words in NSPredicate's language, it has to be escaped using #. To get the height of a view, for example: view.layer.bounds.#size.height.

  2. For collections, keypaths apply to the items inside the collection, not the outer collection itself. To make a key apply to the collection itself, escape the key with @. An example is referring to the count of a collection. The expression subviews.count does not return the number of subviews. Instead, it tries to get the count property of each subview. The @ symbol tells NSPredicate to directly access the count of the container. Referring back to the example, the keypath subviews.@count will return the number of subviews.

For more information on NSPredicate syntax, the "Predicate Programming Guide" has some detail. The list of reserved words is in the "Predicate Format String Syntax" section.

Clone this wiki locally