2010-12-03 3 views
5

어떤 방법이 있습니까? 다른 세트에서 제외 할 항목이 있습니다. 내 세트의 각 항목을 반복 할 수 있고 다른 세트에없는 경우에만 filteredSet에 추가 할 수 있지만 조건부를 사용할 수 있으면 좋을 것입니다.다른 세트에있는 모든 항목을 걸러내는 NSPredicate

제외 할 항목 집합은 같은 유형의 개체 집합을 직접적으로 집합하지 않습니다. 그것은 일련의 문자열입니다. 나는 속성 중 하나가 해당 문자열과 일치하는 경우 즉 .... 내 첫 번째 세트에서 아무것도를 제외 할 :

NSMutableArray *filteredArray = [NSMutableArray arrayWithCapacity:self.questionChoices.count]; 

BOOL found; 

for (QuestionChoice *questionChoice in self.questionChoices) 
{ 
    found = NO; 

    for (Answer *answer in self.answers) 
    { 
     if ([answer.units isEqualToString:questionChoice.code]) 
     { 
      found = YES; 
      break; 
     } 
    } 

    if (!found) 
     [filteredArray addObject:questionChoice]; 
} 

이 대신 술어와 함께 할 수 있습니까?

답변

6

이 술어 형식 문자열 작동합니다 :

@"NONE %@.units == code", self.answers 

해당 NSArray를 필터링 방법으로 결합합니다. self.questions 정규 변경할 수있는 NSArray 경우, 그것은이있는 NSMutableArray을의 경우, 적절한 사용이

[self.questions filterUsingPredicate:predicate]; 

비록 마지막 하나주의 할 것

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NONE %@.units == code", self.answers] 

NSArray *results = [self.questions filteredArrayUsingPredicate:predicate]; 

같이 보일 것입니다, 그것은 기존의 배열을 수정 결과에 맞게. 필요한 경우 어레이의 복사본을 만들어 복사를 피할 수 있습니다.

참조 :
NSArray Class Reference
NSMutableArray Class Reference
Predicate Programming Guide

+0

굉장, "NONE"은 완벽하게 작동했습니다. 제가 놓친 것은 제가 다른 객체와 마찬가지로 당신이 Array 나 Set을 술어 형식으로 전달할 수 있다는 것을 깨닫지 못했다는 것입니다. 감사! – GendoIkari

0

predicates with arrays을 사용하는 Apple의 예제를 확인하십시오. filteredArrayUsingPredicate를 사용합니다.

+0

내가 filteredArrayUsingPredicate를 사용할 필요가 있음을 알고,하지만 난 다른 배열에 특정 값이 포함되어 있는지 술어 확인할 수 방법을 모르겠어요. – GendoIkari

+0

사과, 가려움증 방아쇠. 다음과 같은 일종의 두 번째 술어와 함께 하위 쿼리를 사용할 수 있습니까? http://www.answerspice.com/c119/1665179/can-an-nspredicate-search-for-an-object-within-an-array object-in-another-array가 소유 한 것인가? – jarmod

관련 문제