2012-01-23 2 views
1

코어 데이터 저장소를 쿼리하는 데 NSPredicate를 사용하고 있습니다. NSNumber 유형의 저작물을 보유하고 있습니다. 나는 시도 1.NSPredicate를 사용하여 여러 값 반환

동일 모든 부가 요소를 얻으려면 :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"attribute == %i", i]; 

어디에서 = 1

이 작동하지만 그것은 단지 일치하는 속성을 가진 첫 번째 개체를 반환합니다. 이 속성이 1 인 모든 객체를 원합니다.

어디서 잘못 되었나요? 쿼리 앞에 ALL을 넣으려고했지만 충돌이 발생했습니다.

업데이트 :

이 내 전체 코드입니다 : 다음

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" 
              inManagedObjectContext:managedObjectContext]; 
[fetchRequest setEntity:entity]; 
[fetchRequest setResultType:NSDictionaryResultType]; 

NSNumber *attributeValue = [NSNumber numberWithInt:1]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"attribute == %@", 
          attributeValue]; 
[fetchRequest setPredicate:predicate]; 

NSError *error = nil; 
NSArray *fetchResults = [managedObjectContext executeFetchRequest:fetchRequest 
                   error:&error]; 

return [fetchResults objectAtIndex:0]; 

그리고 :

NSDictionary *dict = [self queryContextWithQuery:nil]; 
NSLog(@"%@", dict); 

로그 만 보여줍니다 1이 표시되어야합니다 3 가능한 객체 중.

답변

0

그것은 문제가 쿼리 아니었다 밝혀졌습니다. 내가 결과를 어떻게 되돌려 놓았는지와 함께했다. 나는 사전 대신 배열을 반환하는 방법을 수정 및 변경 :

return [fetchResults objectAtIndex:0]; 

사람 :

return fetchResults; 

그리고 지금은 올바른 결과를 얻고있다.

0

이 시도 :

NSNumber *attributeValue = [NSNumber numberWithInt:1]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myAttr == %@", 
     attributeValue]; 
+0

술어에 대한 구현되지 않은 SQL 생성 –

+0

@kmcgrady 왜 그런 오류가 발생하는지 확신 할 수없는 ==를 변경하려고합니다. –

+0

원래 코드처럼 작동합니다. 기준과 일치하는 3 대신 1 개의 객체 만 반환합니다. –

0

이 작동합니다 :

NSNumber *attributeValue = [NSNumber numberWithInt:1]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"attribute == %@", 
     attributeValue]; 
+0

나는 그와 같은 결과를 얻고있다. NSDictionary를 로깅 할 때 기준과 일치하는 3을 표시해야 할 때 1 개체 만 표시합니다. –

관련 문제