2017-01-30 2 views
0

연락처 컨테이너 (CNContainer)와 관련된 그룹 목록 (CNGroup)을 얻는 방법을 찾고 있습니다. 내가 predicate을 사용할 때 실패합니다.CNContactStore를 사용하여 CNContainer에서 CNGroup의 배열을 가져 오는 방법은 무엇입니까?

내가 사용하는 코드는

func populateGroups(tableView:NSTableView,container:CNContainer){ 

    print("populateGroups.start") 

    print(container.name) 
    print(container.identifier) 

    let contactStore = CNContactStore() 

    do { 
     let groupsPredicate = CNGroup.predicateForGroups(withIdentifiers: [container.identifier]) 
     groups = try contactStore.groups(matching: groupsPredicate) 
     groupNames.removeAll(); 
     for group:CNGroup in groups { 
      self.groupNames.append(group.name) 
     } 
     tableView.reloadData() 
    } catch { 
     print("Unexpected error fetching groups") 
    } 

    print("populateGroups.finish") 

} 

내가 그에서 오류가 나에게 이해가되지 않습니다 얻고있다.

줄 그룹 = try contactStore.groups (일치 : groupsPredicate)로 인해 오류가 발생합니다. 내가 업데이트 아니에요으로 혼란 오류 도메인 = ABAddressBookErrorDomain 코드 = 1002 "(널)"

:

[계정] 식별자 47008233 - A663-4A52-8487-9D7505847E29, 오류가있는 계정을 업데이트하지 못했습니다 모든 계정.

해당 코드 줄을 groups로 변경하면 contactStore.groups를 입력하십시오 (일치 : 사용하지 않음) 모든 컨테이너에 대해 모든 그룹을 가져옵니다.

CNContactContainer에 속한 모든 CNGroup을 반환하는 조건부를 어떻게 만듭니 까?

답변

0

나는 모든 그룹에서 각 그룹이

func populateGroups(tableView:NSTableView,container:CNContainer){ 

    let contactStore = CNContactStore() 

    do { 
     let groups:[CNGroup] = try contactStore.groups(matching: nil) 
     self.groups.removeAll(); 
     groupNames.removeAll(); 
     for group:CNGroup in groups { 
      let groupContainerPredicate:NSPredicate = CNContainer.predicateForContainerOfGroup(withIdentifier: group.identifier) 
      let groupContainer:[CNContainer] = try contactStore.containers(matching: groupContainerPredicate) 
      if(groupContainer[0].identifier == container.identifier) { 
       self.groupNames.append(group.name) 
       self.groups.append(group) 
      } 
     } 
     tableView.reloadData() 

    } catch { 
     print("Unexpected error fetching groups") 
    } 

} 
CNContainer.predicateForContainerOfGroup

를 사용하여 문제의 컨테이너에 속한 것을 확인하여이 문제를 해결했다