2012-06-11 30 views
6

Tôi có một thực thể dữ liệu cốt lõi, Client, có thuộc tính discount. Tôi muốn tìm nạp khách hàng với mức chiết khấu nhỏ nhất.iOS: Sử dụng @min và @max trong một vị từ dữ liệu chính

Tôi đang sử dụng sau NSPredicate:

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

Tuy nhiên, tôi nhận được lỗi sau:

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

đang gì không làm đúng không?

+0

bạn đang tìm kiếm cái gì là thế này: [Key-Value Mã hóa Programming Guide: Bộ sưu tập nhà khai thác] (http: // nhà phát triển .apple.com/library/ios/# documentation/Cocoa/Khái niệm/KeyValueCoding/Bài viết/Bộ sưu tậpOperators.html) – kimimaro

Trả lời

7

Tôi không nghĩ rằng NSPredicate có hỗ trợ các chức năng như thế này trừ khi nó là một phần của biểu thức vị ngữ boolean (tức là liên quan đến những thứ như "lớn hơn").

Bạn nên đọc this CoreData documentation mang đến cho một số ví dụ, đặc biệt là sử dụng max làm ví dụ:

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]]; 
Các vấn đề liên quan