美文网首页
Predicate Programming Guide

Predicate Programming Guide

作者: ngugg | 来源:发表于2018-10-25 16:57 被阅读12次

    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谓词的定义。

    相关文章

      网友评论

          本文标题:Predicate Programming Guide

          本文链接:https://www.haomeiwen.com/subject/jtlctqtx.html