首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CNContactStore从CNContainer中获取CNGroup数组?

如何使用CNContactStore从CNContainer中获取CNGroup数组?
EN

Stack Overflow用户
提问于 2017-01-30 09:08:39
回答 1查看 1K关注 0票数 0

我正在寻找一种方法来获取与联系人容器(CNContainer)相关的组列表(CNGroup)。当我使用predicate时,它失败了。

我使用的代码是

代码语言:javascript
复制
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的谓词?

EN

回答 1

Stack Overflow用户

发布于 2017-01-30 13:26:45

我通过使用CNContainer.predicateForContainerOfGroup检查所有组中的每个组是否属于有问题的容器来解决这个问题

代码语言:javascript
复制
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")
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41927758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档