2012-06-11 3 views
6

코어 데이터 엔티티 Client이 있는데, discount 속성이 있습니다. 나는 가장 작은 할인을 가진 클라이언트를 가져오고 싶다.iOS : 코어 데이터 술어에서 @min과 @max를 사용합니다.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "@min.discount"' 

잘 수 없어요 무엇을 : 나는 다음과 같은 오류를 얻고있다, 그러나

[NSPredicate predicateWithFormat:@"@min.discount"]; 

:

나는 NSPredicate 다음을 사용하고?

+0

이있다 (HTTP : // 개발자 .apple.com/library/ios/# documentation/코코아/개념/KeyValueCoding/Articles/CollectionOperators.html) – kimimaro

답변

7

NSPredicate이 부울 조건부 표현식의 일부 (즉, "보다 큼"과 같은 경우)가 아니면 이러한 기능을 지원하지 않는다고 생각합니다. 당신은 구체적으로 예를 들어 max를 사용하여, 몇 가지 예를 제공하는 this CoreData documentation을 읽어야

: [키 - 값 코딩 프로그래밍 가이드 : 컬렉션 연산자] 당신이 찾고있는 무엇

There are a number of steps to follow to create and use the expression description.

First you need to create expressions (instances of NSExpression) to represent the key-path for the value you’re interested in, and to represent the function you want to apply (such as max: or min:):

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"salary"]; 
NSExpression *maxSalaryExpression = [NSExpression expressionForFunction:@"max:" 
                arguments:[NSArray arrayWithObject:keyPathExpression]]; 

For a full list of supported functions, see expressionForFunction:arguments: .

You then create the expression description and set its name, expression, and result type.

The name is the key that will be used in the dictionary for the return value. If you want to retrieve multiple values—such as the largest and the smallest salaries in an Employee table—the name of each expression description must be unique for a given fetch request.

NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 
[expressionDescription setName:@"maxSalary"]; 
[expressionDescription setExpression:maxSalaryExpression]; 
[expressionDescription setExpressionResultType:NSDecimalAttributeType]; 

Finally, you set the request’s properties to fetch just the property represented by the expression:

[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];