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中指定查询的一般方法。 谓词系统能够处理大量域,包括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中,谓词是一个逻辑语句,其值为布尔值(true或false)。 谓词有两种类型,比较复合
  • 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.
  • 比较谓词使用运算符比较两个表达式。 表达式被称为谓词的左侧和右侧(操作符位于中间)。 比较谓词返回调用运算符的结果以及计算表达式的结果。
  • A compound predicate compares the results of evaluating two or more other predicates, or negates another predicate.
  • 复合谓词比较评估两个或多个其他谓词的结果,或否定另一个谓词。

Cocoa supports a wide range of types of predicate, including the following:
Cocoa支持各种类型的谓词,包括以下内容:

  • 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'
  • 大小写或变音符号不敏感的查找,例如 name contains[cd]'citroen'
  • Logical operations, such as (firstName beginswith 'M') AND (lastName like 'Adderley')
  • 逻辑运算,例如(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.

  • 您还可以为关系创建谓词 - 例如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谓词提供了一种以独立于用于保存被搜索数据的存储的方式对查询进行编码的方法。 您可以使用谓词来表示用于约束Spotlight和Core Data检索的对象集以及对象的内存中过滤的逻辑条件。

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.

  • 您可以将谓词与任何类对象一起使用,但是类必须是符合要在谓词中使用的键的键值编码。

Predicate Classes

Cocoa provides NSPredicate and its two subclasses, NSComparisonPredicate and NSCompoundPredicate.

  • Cocoa提供了NSPredicate及其两个子类,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类提供了评估谓词和从字符串创建谓词的方法(例如firstName,如'Mark')。 从字符串创建谓词时,NSPredicate会为您创建适当的谓词和表达式实例。 在某些情况下,您希望自己创建比较或复合谓词,在这种情况下,您可以使用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中的谓词表达式由NSExpression类的实例表示。 最简单的谓词表达式表示常量值。 但是,通常使用表达式来检索谓词中当前正在评估的对象的键路径的值。 您还可以创建表达式来表示谓词中当前正在评估的对象,充当变量的占位符,或返回对数组执行操作的结果。

For more on creating predicates and expressions, see Creating Predicates.

  • 有关创建谓词和表达式的更多信息,请参阅创建谓词。

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一起使用,请注意它们与相应的数据存储一起使用。 谓词查询没有特定的实现语言; 谓词查询可以转换为SQL,XML或其他格式,具体取决于后备存储的要求(如果确实有一个)。

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谓词系统旨在支持有用的运算符范围,因此既不提供集合联合,也不提供所有后备存储支持的所有运算符的集合交集。 因此,并非所有后备存储都支持所有可能的谓词查询,并且并非所有后备存储支持的所有操作都可以使用NSPredicate和NSExpression对象表示。 后端可能会降级谓词(例如,它可能使区分大小写的比较不区分大小写)或者如果您尝试使用不受支持的运算符则引发异常。 例如:

  • The matches operator uses regex, so is not supported by Core Data’s SQL store— although it does work with in-memory filtering.

    • 匹配运算符使用正则表达式,因此Core Data的SQL存储不支持 - 尽管它可以与内存中过滤一起使用。
  • 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存储每个查询仅支持一对多操作; 因此,在发送到SQL存储的任何谓词中,ALL,ANY和IN中可能只有一个运算符(以及该运算符的一个实例)。
  • You cannot necessarily translate arbitrary SQL queries into predicates.

    • 您不一定能将任意SQL查询转换为谓词。
  • The ANYKEY operator can only be used with Spotlight.

    • ANYKEY运算符只能与Spotlight一起使用。
  • Spotlight does not support relationships.

    • Spotlight不支持关系。

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中谓词的基础知识,解释了如何创建和使用谓词对象,以及定义谓词语法:

  • Creating Predicates describes how to correctly instantiate predicates programmatically and how to retrieve them from a managed object model.

    • 创建谓词描述了如何以编程方式正确实例化谓词以及如何从托管对象模型中检索它们。
  • Using Predicates explains how to use predicates and discusses some issues related to performance.

    • 使用谓词解释了如何使用谓词并讨论了与性能相关的一些问题。
  • 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谓词的定义。
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容