【问题标题】:Which keys do I need for CNContactFormatter?CNContactFormatter 需要哪些键?
【发布时间】:2016-01-27 19:30:32
【问题描述】:

我正在尝试使用新的CNContactFormatter 格式化联系人姓名。看起来,我没有获取联系人的所有需要​​的姓名属性。

Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.'

有谁知道需要哪些?我尝试在其他几个没有运气的情况下获取以下内容:

        CNContactNamePrefixKey,
        CNContactGivenNameKey,
        CNContactFamilyNameKey,
        CNContactMiddleNameKey, 
        CNContactPreviousFamilyNameKey,
        CNContactNameSuffixKey,
        CNContactNicknameKey,
        CNContactPhoneticGivenNameKey,
        CNContactPhoneticMiddleNameKey,
        CNContactPhoneticFamilyNameKey,
        CNContactOrganizationNameKey,
        CNContactDepartmentNameKey,
        CNContactJobTitleKey,

CNContactFomatter Class Referencefetching method's documentation 都没有提供任何线索。

谢谢!

【问题讨论】:

    标签: formatting ios9 cncontact cncontactformatter cncontactstore


    【解决方案1】:

    我在 WWDC Session 223(从幻灯片 74 开始)中发现了这一点,当我遇到同样的问题时,这对我有用。在联系人选择调用中使用 CNContactFormatter.descriptorForRequiredKeysForStyle...。示例:

    let contactStore = CNContactStore()
    let predicate = CNContact.predicateForContactsMatchingName("John")
    let foundContacts = try contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName)]
    for contact in foundContacts {
                print(CNContactFormatter.stringFromContact(contact, style: .FullName))
    }
    

    【讨论】:

    • 天哪,谢谢!这就像一个魅力,甚至与一些额外的领域相结合。
    • 哇。深切。非常感谢您分享这个解决方案!
    【解决方案2】:
    class func descriptorForRequiredKeys()
    

    用于获取从联系人创建 vCard 数据所需的所有联系人密钥。

    https://developer.apple.com/reference/contacts/cncontactvcardserialization

    例子:

    let containerResults =  try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch:[CNContactVCardSerialization.descriptorForRequiredKeys()])
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 2017-10-28
    • 2021-07-06
    • 2018-10-22
    • 2012-06-08
    • 2013-10-20
    • 2014-08-28
    相关资源
    最近更新 更多