2012-10-29 6 views
1

문자열과 객체 세트와 일치하는 객체를 찾으려고합니다. 내 술어는 다음과 같습니다.코어 데이터 술어 - 다수

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ and individuals CONTAINS %@", name, individuals]; 

나는 어떤 조회가 발생하지 않습니다. 나는 이름과 개인이 일치하는 엔티티가 있음을 알고 있지만.

내 조건부에 어떤 문제가 있습니까?

편집 : 일부 작업을 수행했습니다. 문제는 현재 기존 그룹 이름과 기존 연락처 (예 : groupname = "test"및 individuals.name = "john doe"및 individuals.contactInfo = "123")가있는 그룹을 찾으면이 그룹을 올바르게 찾을 수 있습니다. 하지만 같은 그룹 이름과 같은 연락처 + 다른 연락처를 가진 그룹이 있다면 그것은 내가 원하지 않는이 그룹을 찾게 될 것입니다.

전 정확히 술어와 일치하는 그룹 만 원합니다.

나는 지금 이렇게 subpredicates을 사용하고 있습니다 :

NSMutableArray *subPredicates = [NSMutableArray initWithCapacity:5]; 
// Add group name to predicate 
[subPredicates addObject:[NSPredicate predicateWithFormat:@"name == %@", name]]; 

for (NSDictionary *contactInfo in individuals) { 

    NSString *name = [contactDict objectForKey:@"name"]; 
    NSString *contactInfo = [contactDict objectForKey:@"contactInfo"]; 

    NSPredicate *individualPredicate = [NSPredicate predicateWithFormat:@"ANY individuals.name LIKE %@ AND any individuals.contactInfo LIKE %@", name, contactInfo]; 

    [subPredicates addObject:individualPredicate]; 
} 

NSPredicate *individualsPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates]; 

Group *group = // do the fetch with the predicate 

답변

1

나는 전화에 입력하고 그것을 확인할 수는 없지만, 다음과 같은 조건이 당신을 위해 일 수 있습니다 :

[NSPredicate predicateWithFormat:@"name == %@ AND SUBQUERY(individuals, $x, $x IN %@)[email protected] == %d", 
    name, individuals, individuals.count]; 

그것은 검색 당신은 어떤 술어를 사용하여 세트에서 하나의 개체를 일치시킬 수 있습니다 지정된 이름의 객체가 그 세트의 슈퍼 세트 인 객체의 경우

+0

SUBQUERY를 사용하여 술어를 기반으로하는 콜렉션을 필터링 할 수 있다고 생각했습니다. Apple의 예 : (SUBQUERY (거주자, $ x, $ x.firstname == "Jane"&& $ x.lastname == "Doe"). @ count! = 0). 주어진 요소 목록에서 작동하지 않을 것이라고 생각합니다. – diederikh

+0

이 술어가 저에게 효과적이지 않았습니다. 두 명이 포함 된 그룹이있는 경우,이 그룹의 사람 중 하나만 제공 했더라도이 술어는이 그룹을 리턴 할 것입니다. –

+0

나는이 질문에 대한 답을 다른 답으로 찾았으므로이 답을 받아 들일 수 있도록 표시 할 것입니다. (대답 : http://stackoverflow.com/a/13086353/294661) –

3

개체 집합이 CONTAINS 조건과 일치 할 수 없습니다.

[NSPredicate predicateWithFormat:@"name == %@ and ANY individuals.name == %@", name, individualName]; 
+1

그래서 'NSSet'을 술어와 일치시킬 방법이 없습니까? –

+0

거의 제 생각에는 제 코드가 업데이트되었습니다. 마지막 작품으로 나를 도울 수 있니? –

+0

세트의 모든 요소를 ​​일치 시키려면 ANY 대신 ALL 술어를 사용할 수 있습니다. – diederikh