2010-06-12 2 views
0

documentation에는 관리되는 개체가 아닌 단순한 값만 검색하는 방법에 대한 예제가 있습니다. 별칭과 함수를 사용하여 계산 된 값만 검색하는 SQL을 많이 기억합니다. 그래서, 실제로 꽤 귀찮은 물건. 기록의 무리에서 최소 날짜를 얻으려면,이 맥에 사용됩니다iPhone에서 이와 같은 표현식을 사용하여 가져 오기 요청을 수행하는 방법은 무엇입니까?

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context]; 
[request setEntity:entity]; 

// Specify that the request should return dictionaries. 
[request setResultType:NSDictionaryResultType]; 

// Create an expression for the key path. 
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"creationDate"]; 

// Create an expression to represent the minimum value at the key path 'creationDate' 
NSExpression *minExpression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:keyPathExpression]]; 

// Create an expression description using the minExpression and returning a date. 
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 

// The name is the key that will be used in the dictionary for the return value. 
[expressionDescription setName:@"minDate"]; 
[expressionDescription setExpression:minExpression]; 
[expressionDescription setExpressionResultType:NSDateAttributeType]; 

// Set the request's properties to fetch just the property represented by the expressions. 
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; 

// Execute the fetch. 
NSError *error; 
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error]; 
if (objects == nil) { 
    // Handle the error. 
} 
else { 
    if ([objects count] > 0) { 
     NSLog(@"Minimum date: %@", [[objects objectAtIndex:0] valueForKey:@"minDate"]; 
    } 
} 

[expressionDescription release]; 
[request release]; 

니스,하지만 I -하지만 아이폰 OS가 min: 기능을 지원하지 않는 것으로 나타났다 NSExpression -expressionForFunction:arguments:에 깊은 모양을 가진.

음, 아마도 iPhone의 이러한 종류의 기능에 고유 기능을 사용하는 멋진 방법이 있을까요? 내가 이미 염려하고있는 점은지도에서 계산 된 목표 거리 (위치 기반 자료)를 기반으로 표를 정렬하는 방법입니다.

답변

1

당신은, 당신의 자신의 기능을 구현이 지정된 인수를 사용하여 주어진 이름으로 주어진 대상에 선택기를 호출 한 결과를 반환하는 식을 반환

+ (NSExpression *)expressionForFunction:(NSExpression *)target selectorName:(NSString *)name arguments:(NSArray *)parameters 

사용할 수 있습니다.

0

thnx는

@dontwatchmyprofile 난 그냥

NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]]; 

는 아이폰 OS 3.2 SDK와 함께 잘 동작하는 것 테스트.

관련 문제