Introduction
Predicates provide a general means of specifying queries in Cocoa. The predicate system is capable of handling a large number of domains, including Core Data and Spotlight. This document describes predicates in general, their use, their syntax, and their limitations.
- 謂詞提供了在Cocoa中指定查詢的一般方法锌畸。 謂詞系統(tǒng)能夠處理大量域,包括Core Data和Spotlight靖避。 本文檔描述了一般的謂詞潭枣,它們的用法比默,語法和限制。
At a Glance
In Cocoa, a predicate is a logical statement that evaluates to a Boolean value (true or false). There are two types of predicate, known as comparison and compound:
- 在Cocoa中盆犁,謂詞是一個(gè)邏輯語句命咐,其值為布爾值(true或false)。 謂詞有兩種類型谐岁,
比較
和復(fù)合
:
- A comparison predicate compares two expressions using an operator. The expressions are referred to as the left hand side and the right hand side of the predicate (with the operator in the middle). A comparison predicate returns the result of invoking the operator with the results of evaluating the expressions.
-
比較謂詞
使用運(yùn)算符
比較兩個(gè)表達(dá)式
侈百。 表達(dá)式被稱為謂詞的左側(cè)和右側(cè)(操作符位于中間)。 比較謂詞返回調(diào)用運(yùn)算符的結(jié)果以及計(jì)算表達(dá)式的結(jié)果翰铡。
- A compound predicate compares the results of evaluating two or more other predicates, or negates another predicate.
-
復(fù)合謂詞
比較評(píng)估兩個(gè)或多個(gè)其他謂詞的結(jié)果,或否定另一個(gè)謂詞讽坏。
Cocoa supports a wide range of types of predicate, including the following:
Cocoa支持各種類型的謂詞锭魔,包括以下內(nèi)容:
- Simple comparisons, such as
grade == 7
orfirstName like 'Mark'
- 簡單的比較,例如grade == 7或firstName是不是'Mark'
- Case or diacritic insensitive lookups, such as
name contains[cd] 'citroen'
- 大小寫或變音符號(hào)不敏感的查找路呜,例如 name contains[cd]'citroen'
- Logical operations, such as
(firstName beginswith 'M') AND (lastName like 'Adderley')
- 邏輯運(yùn)算迷捧,例如(firstName以'M'開頭)AND(lastName like'Adderley')
You can also create predicates for relationships—such as group.name matches 'work.*'
, ALL children.age > 12
, and ANY children.age > 12
—and for operations such as @sum.items.price < 1000
.
- 您還可以為關(guān)系創(chuàng)建謂詞 - 例如group.name匹配'work.*',ALL children.age> 12胀葱,ANY children.age> 12 - 以及@ sum.items.price <1000等操作漠秋。
Cocoa predicates provide a means of encoding queries in a manner that is independent of the store used to hold the data being searched. You use predicates to represent logical conditions used for constraining the set of objects retrieved by Spotlight and Core Data, and for in-memory filtering of objects.
- Cocoa謂詞提供了一種以獨(dú)立于用于保存被搜索數(shù)據(jù)的存儲(chǔ)的方式對(duì)查詢進(jìn)行編碼的方法。 您可以使用謂詞來表示用于約束Spotlight和Core Data檢索的對(duì)象集以及對(duì)象的內(nèi)存中過濾的邏輯條件抵屿。
You can use predicates with any class of object, but a class must be key-value coding compliant for the keys you want to use in a predicate.
- 您可以將謂詞與任何類對(duì)象一起使用庆锦,但是類必須是符合要在謂詞中使用的鍵的鍵值編碼。
Predicate Classes
Cocoa provides NSPredicate and its two subclasses, NSComparisonPredicate and NSCompoundPredicate.
- Cocoa提供了NSPredicate及其兩個(gè)子類轧葛,NSComparisonPredicate和NSCompoundPredicate搂抒。
The NSPredicate class provides methods to evaluate a predicate and to create a predicate from a string (such as firstName like 'Mark'
). When you create a predicate from a string, NSPredicate
creates the appropriate predicate and expression instances for you. In some situations, you want to create comparison or compound predicates yourself, in which case you can use the NSComparisonPredicate
and NSCompoundPredicate
classes.
- NSPredicate類提供了評(píng)估謂詞和從字符串創(chuàng)建謂詞的方法(例如firstName,如'Mark')尿扯。 從字符串創(chuàng)建謂詞時(shí)求晶,NSPredicate會(huì)為您創(chuàng)建適當(dāng)?shù)闹^詞和表達(dá)式實(shí)例。 在某些情況下衷笋,您希望自己創(chuàng)建比較或復(fù)合謂詞芳杏,在這種情況下,您可以使用NSComparisonPredicate和NSCompoundPredicate類辟宗。
Predicate expressions in Cocoa are represented by instances of the NSExpression class. The simplest predicate expression represents a constant value. Frequently, though, you use expressions that retrieve the value for a key path of the object currently being evaluated in the predicate. You can also create an expression to represent the object currently being evaluated in the predicate, to serve as a placeholder for a variable, or to return the result of performing an operation on an array.
- Cocoa中的謂詞表達(dá)式由NSExpression類的實(shí)例表示爵赵。 最簡單的謂詞表達(dá)式表示常量值。 但是泊脐,通常使用表達(dá)式來檢索謂詞中當(dāng)前正在評(píng)估的對(duì)象的鍵路徑的值亚再。 您還可以創(chuàng)建表達(dá)式來表示謂詞中當(dāng)前正在評(píng)估的對(duì)象,充當(dāng)變量的占位符晨抡,或返回對(duì)數(shù)組執(zhí)行操作的結(jié)果氛悬。
For more on creating predicates and expressions, see Creating Predicates.
- 有關(guān)創(chuàng)建謂詞和表達(dá)式的更多信息则剃,請(qǐng)參閱創(chuàng)建謂詞。
Constraints and Limitations 約束和限制
If you use predicates with Core Data or Spotlight, take care that they work with the corresponding data store. There is no specific implementation language for predicate queries; a predicate query may be translated into SQL, XML, or another format, depending on the requirements of the backing store (if indeed there is one).
- 如果將謂詞與Core Data或Spotlight一起使用如捅,請(qǐng)注意它們與相應(yīng)的數(shù)據(jù)存儲(chǔ)一起使用棍现。 謂詞查詢沒有特定的實(shí)現(xiàn)語言; 謂詞查詢可以轉(zhuǎn)換為SQL,XML或其他格式镜遣,具體取決于后備存儲(chǔ)的要求(如果確實(shí)有一個(gè))己肮。
The Cocoa predicate system is intended to support a useful range of operators, so provides neither the set union nor the set intersection of all operators supported by all backing stores. Therefore, not all possible predicate queries are supported by all backing stores, and not all operations supported by all backing stores can be expressed with NSPredicate
and NSExpression
objects. The back end might downgrade a predicate (for example it might make a case-sensitive comparison case-insensitive) or raise an exception if you try to use an unsupported operator. For example:
-Cocoa謂詞系統(tǒng)旨在支持有用的運(yùn)算符范圍,因此既不提供集合聯(lián)合悲关,也不提供所有后備存儲(chǔ)支持的所有運(yùn)算符的集合交集谎僻。 因此,并非所有后備存儲(chǔ)都支持所有可能的謂詞查詢寓辱,并且并非所有后備存儲(chǔ)支持的所有操作都可以使用NSPredicate和NSExpression對(duì)象表示艘绍。 后端可能會(huì)降級(jí)謂詞(例如,它可能使區(qū)分大小寫的比較不區(qū)分大小寫)或者如果您嘗試使用不受支持的運(yùn)算符則引發(fā)異常秫筏。 例如:
-
The
matches
operator usesregex
, so is not supported by Core Data’s SQL store— although it does work with in-memory filtering.- 匹配運(yùn)算符使用正則表達(dá)式诱鞠,因此Core Data的SQL存儲(chǔ)不支持 - 盡管它可以與內(nèi)存中過濾一起使用。
-
The Core Data SQL store supports only one to-many operation per query; therefore in any predicate sent to the SQL store, there may be only one operator (and one instance of that operator) from
ALL
,ANY
, andIN
.- Core Data SQL存儲(chǔ)每個(gè)查詢僅支持一對(duì)多操作; 因此这敬,在發(fā)送到SQL存儲(chǔ)的任何謂詞中航夺,ALL,ANY和IN中可能只有一個(gè)運(yùn)算符(以及該運(yùn)算符的一個(gè)實(shí)例)崔涂。
-
You cannot necessarily translate arbitrary SQL queries into predicates.
- 您不一定能將任意SQL查詢轉(zhuǎn)換為謂詞。
-
The
ANYKEY
operator can only be used with Spotlight.- ANYKEY運(yùn)算符只能與Spotlight一起使用。
-
Spotlight does not support relationships.
- Spotlight不支持關(guān)系涮俄。
How to Use This Document
The following articles explain the basics of predicates in Cocoa, explain how to create and use predicate objects, and define the predicate syntax:
-以下文章解釋了Cocoa中謂詞的基礎(chǔ)知識(shí),解釋了如何創(chuàng)建和使用謂詞對(duì)象苞尝,以及定義謂詞語法:
-
Creating Predicates describes how to correctly instantiate predicates programmatically and how to retrieve them from a managed object model.
- 創(chuàng)建謂詞描述了如何以編程方式正確實(shí)例化謂詞以及如何從托管對(duì)象模型中檢索它們。
-
Using Predicates explains how to use predicates and discusses some issues related to performance.
- 使用謂詞解釋了如何使用謂詞并討論了與性能相關(guān)的一些問題大咱。
-
Comparison of NSPredicate and Spotlight Query Strings compares NSPredicate and Spotlight queries.
- NSPredicate和Spotlight查詢字符串的比較比較了NSPredicate和Spotlight查詢碴巾。
-
Predicate Format String Syntax describes the syntax of the predicate format string.
- 謂詞格式字符串語法描述謂詞格式字符串的語法。
-
BNF Definition of Cocoa Predicates provides a definition of Cocoa predicates in Backus-Naur Form notation.
- BNF Cocoa Predicates的定義提供了Backus-Naur形式表示法中Cocoa謂詞的定義煮仇。