본문 바로가기

IOS 개발

[iOS] Predicate Programming Guide

소개

Introduction

 

술어는 Cocoa에서 쿼리를 지정하는 일반적인 수단을 제공합니다. 술어 시스템은 코어 데이터 및 스포트라이트를 포함하여 많은 수의 도메인을 처리 할 수 있습니다. 이 문서는 일반적으로 술어, 사용법, 구문 및 제한 사항에 대해 설명합니다.

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.

 

한눈에

At a Glance

 

Cocoa에서 술어는 부울 값 (true 또는 false)으로 평가되는 논리 문입니다. 비교 및 복합으로 알려진 두 가지 유형의 술어가 있습니다.

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:

 

비교 술어는 연산자를 사용하여 두 표현식을 비교합니다. 표현식을 술어의 왼쪽과 오른쪽이라고합니다 (중간에 연산자가 있음). 비교 술어는 연산자 평가 결과와 표현식 평가 결과를 리턴합니다.

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:

 

등급 == 7 또는 '마크'와 같은 firstName과 같은 간단한 비교입니다.

Simple comparisons, such as grade == 7 or firstName like 'Mark'



이름 등 대/소문자를 구분하지 않는 조회에는 'cd' 'citroen'이 포함됩니다.

Case or diacritic insensitive lookups, such as name contains[cd] 'citroen'



(firstName은 'M' AND('Aderley'와 같은 마지막 이름)와 같은 논리적 작업을 시작합니다.

Logical operations, such as (firstName beginswith 'M') AND (lastName like 'Adderley')

 

또한 group,name이  'work. *'와 일치하는 관계, ALL children.age> 12 및 ANY children.age> 12와 같은 관계 및 @ sum.items.price <1000과 같은 조작에 대한 술어를 작성할 수도 있습니다.

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.

 

Cocoa 술어는 검색중인 데이터를 보유하는 데 사용되는 상점과 독립적 인 방식으로 조회를 인코딩하는 수단을 제공합니다. 술어를 사용하여 Spotlight 및 Core Data에 의해 검색된 오브젝트 세트를 제한하고 오브젝트의 메모리 내 필터링에 사용되는 논리 조건을 나타냅니다.

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.

 

모든 클래스의 오브젝트에 술어를 사용할 수 있지만, 클래스는 술어에 사용하려는 키에 대해 키-값 코딩을 준수해야합니다.

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

 

코코아는 NSPredicate 및 NSComparisonPredicate와 NSCompoundPredicate라는 두 개의 하위 클래스를 제공합니다.

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

 

NSPredicate 클래스는 술어를 평가하고 문자열 (예 : 'Mark'와 같은 firstName)에서 술어를 작성하는 메소드를 제공합니다. 문자열에서 조건자를 만들면 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.

 

Cocoa의 술어 표현식은 NSExpression 클래스의 인스턴스로 표시됩니다. 가장 간단한 술어 표현식은 상수 값을 나타냅니다. 그러나 현재 술어에서 현재 평가중인 오브젝트의 키 경로에 대한 값을 검색하는 표현식을 사용합니다. 또한 술어에서 현재 평가중인 오브젝트를 나타내거나 변수의 플레이스 홀더로 사용하거나 배열에서 조작 수행 결과를 리턴하는 표현식을 작성할 수도 있습니다.

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.

술어 및 표현식 작성에 대한 자세한 정보는 술어 작성을 참조하십시오.

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

 

 

 

 

 

 

제약 조건 및 제한 사항

Constraints and Limitations

 

Core Data 또는 Spotlight와 함께 술어를 사용하는 경우 해당 데이터 저장소에서 작동하는지 주의하십시오. 술어 쿼리에 대한 특정 구현 언어는 없습니다. 술어 쿼리는 백업 저장소의 요구 사항에 따라 SQL, XML 또는 다른 형식으로 변환 될 수 있습니다 (사실 있는 경우).

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).

 

Cocoa 술어 시스템은 유용한 연산자 범위를 지원하기 위해 모든 공용 상점에서 지원하는 모든 연산자의 집합 조합이나 집합 교차를 제공하지 않습니다. 따라서 가능한 모든 술어 쿼리가 모든 백업 저장소에서 지원되는 것은 아니며 모든 백업 저장소가 지원하는 모든 작업을 NSPredicate 및 NSExpression 객체로 표현할 수있는 것은 아닙니다. 지원되지 않는 연산자를 사용하려고하면 백엔드는 술어를 다운 그레이드하거나 (예 : 대소 문자를 구분하지 않는 비교를 만들 수 있음) 예외를 발생시킬 수 있습니다. 예를 들면 다음과 같습니다.

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:

 

match 연산자는 정규식을 사용하므로 메모리 내 필터링에서는 작동하지만 Core Data의 SQL 저장소에서는 지원되지 않습니다.

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 저장소는 쿼리 당 하나의 다 대일 작업 만 지원합니다. 따라서 SQL 스토어로 전송 된 술어에는 ALL, ANY 및 IN의 연산자 (및 해당 연산자의 인스턴스 하나) 만있을 수 있습니다.

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.



임의의 SQL 쿼리를 술어로 변환 할 필요는 없습니다.

You cannot necessarily translate arbitrary SQL queries into predicates.



ANYKEY 연산자는 Spotlight에서만 사용할 수 있습니다.

The ANYKEY operator can only be used with Spotlight.



스포트라이트는 관계를 지원하지 않습니다.

Spotlight does not support relationships.

 

 

 

 

 

이 문서를 사용하는 방법

How to Use This Document

 

 

다음 기사는 Cocoa에서 술어의 기본 사항을 설명하고 술어 오브젝트를 작성하고 사용하는 방법을 설명하며 술어 구문을 정의합니다.

The following articles explain the basics of predicates in Cocoa, explain how to create and use predicate objects, and define the predicate syntax:

 

 

술어 작성은 프로그래밍 방식으로 술어를 올바르게 인스턴스화하는 방법 및 관리 오브젝트 모델에서 술어를 검색하는 방법을 설명합니다.

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.




NSPredicate 및 Spotlight 쿼리 문자열 비교 NSPredicate 및 Spotlight 쿼리를 비교합니다.

Comparison of NSPredicate and Spotlight Query Strings compares NSPredicate and Spotlight queries.

 



술어 형식 문자열 구문은 술어 형식 문자열의 구문을 설명합니다.

Predicate Format String Syntax describes the syntax of the predicate format string.

 



Cocoa 술어의 BNF 정의 Backus-Naur 양식 표기법으로 Cocoa 술어의 정의를 제공합니다.

BNF Definition of Cocoa Predicates provides a definition of Cocoa predicates in Backus-Naur Form notation.

 

 

 

 

 

 

[참조:  애플공식문서 developer.apple.com]