Predicate Programming Guide

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 or firstName 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 uses regex, 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, and IN.

    • 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謂詞的定義煮仇。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末强重,一起剝皮案震驚了整個(gè)濱河市间景,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌封拧,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,602評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡仔涩,警方通過查閱死者的電腦和手機(jī)熔脂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來袍暴,“玉大人,你說我怎么就攤上這事淋样。” “怎么了儡司?”我有些...
    開封第一講書人閱讀 152,878評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵酵镜,是天一觀的道長垢粮。 經(jīng)常有香客問我足丢,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,306評(píng)論 1 279
  • 正文 為了忘掉前任氮帐,我火速辦了婚禮,結(jié)果婚禮上参咙,老公的妹妹穿的比我還像新娘两入。我一直安慰自己择葡,他們只是感情好她我,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,330評(píng)論 5 373
  • 文/花漫 我一把揭開白布迫横。 她就那樣靜靜地躺著番舆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪矾踱。 梳的紋絲不亂的頭發(fā)上恨狈,一...
    開封第一講書人閱讀 49,071評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音呛讲,去河邊找鬼禾怠。 笑死,一個(gè)胖子當(dāng)著我的面吹牛贝搁,可吹牛的內(nèi)容都是我干的吗氏。 我是一名探鬼主播,決...
    沈念sama閱讀 38,382評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼雷逆,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼弦讽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,006評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤往产,失蹤者是張志新(化名)和其女友劉穎被碗,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體仿村,經(jīng)...
    沈念sama閱讀 43,512評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡锐朴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,965評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蔼囊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片焚志。...
    茶點(diǎn)故事閱讀 38,094評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖压真,靈堂內(nèi)的尸體忽然破棺而出娩嚼,到底是詐尸還是另有隱情,我是刑警寧澤滴肿,帶...
    沈念sama閱讀 33,732評(píng)論 4 323
  • 正文 年R本政府宣布岳悟,位于F島的核電站,受9級(jí)特大地震影響泼差,放射性物質(zhì)發(fā)生泄漏贵少。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,283評(píng)論 3 307
  • 文/蒙蒙 一堆缘、第九天 我趴在偏房一處隱蔽的房頂上張望滔灶。 院中可真熱鬧,春花似錦吼肥、人聲如沸录平。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽斗这。三九已至,卻和暖如春啤斗,著一層夾襖步出監(jiān)牢的瞬間表箭,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評(píng)論 1 262
  • 我被黑心中介騙來泰國打工钮莲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留免钻,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,536評(píng)論 2 354
  • 正文 我出身青樓崔拥,卻偏偏與公主長得像极舔,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子链瓦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,828評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,294評(píng)論 0 10
  • 眾所周知,無論過去還是現(xiàn)在稽揭,成功的很大程度不僅僅取決于你懂得什么俺附,而在于你認(rèn)識(shí)誰。 在今天的商業(yè)環(huán)境之下溪掀,你要成功...
    孤鶩落霞閱讀 531評(píng)論 0 1
  • 每個(gè)人都會(huì)有自己的圈子事镣,都會(huì)有各類的朋友,但每個(gè)人衡量朋友的標(biāo)準(zhǔn)又是不同的揪胃,所以我們很難從日常的交往中定義真正的朋...
    一葉之秋KC閱讀 220評(píng)論 0 2
  • 作者點(diǎn)出人們?cè)谕顿Y領(lǐng)域失敗的最根本原因是貪婪喊递。貪婪源于人性弱點(diǎn)随闪,而投資領(lǐng)域的貪婪很多時(shí)候來自于無知。 首先作者講到...
    Yun_bf83閱讀 429評(píng)論 0 0