CNContactFormatter需要哪些密钥?

flo*_*hei 15 formatting ios9 cncontact cncontactstore cncontactformatter

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

Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.'
Run Code Online (Sandbox Code Playgroud)

有谁知道哪些是必需的?我尝试在其他几个人中抓住以下内容而没有运气:

        CNContactNamePrefixKey,
        CNContactGivenNameKey,
        CNContactFamilyNameKey,
        CNContactMiddleNameKey, 
        CNContactPreviousFamilyNameKey,
        CNContactNameSuffixKey,
        CNContactNicknameKey,
        CNContactPhoneticGivenNameKey,
        CNContactPhoneticMiddleNameKey,
        CNContactPhoneticFamilyNameKey,
        CNContactOrganizationNameKey,
        CNContactDepartmentNameKey,
        CNContactJobTitleKey,
Run Code Online (Sandbox Code Playgroud)

无论是CNContactFomatter类参考,也没有取出方法的文档提供任何线索.

谢谢!

小智 17

我在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))
}
Run Code Online (Sandbox Code Playgroud)