我正在寻找一种方法来获取与联系人容器(CNContainer)相关的组列表(CNGroup)。当我使用predicate时,它失败了。
我使用的代码是
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")
}我得到了一个错误,这对我来说没有意义。
groups = try contactStore.groups(matching: groupsPredicate)行导致错误。
帐户无法更新标识符为47008233的帐户-A663-4A52-8487-9D7505847E29,错误: Error Domain=ABAddressBookErrorDomain Code=1002 "(null)“
这很令人困惑,因为我没有更新任何帐户。
如果我将代码行更改为groups = try contactStore.groups(matching: nil),我将获得所有容器的所有组。
如何创建将返回属于某个CNContactContainer的所有CNGroups的谓词?
发布于 2017-01-30 13:26:45
我通过使用CNContainer.predicateForContainerOfGroup检查所有组中的每个组是否属于有问题的容器来解决这个问题
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")
}
}https://stackoverflow.com/questions/41927758
复制相似问题