我尝试使用新的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 Reference和fetching method's documentation都没有任何线索。
谢谢!
答案 0 :(得分: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))
}
答案 1 :(得分:1)
class func descriptorForRequiredKeys()
用于获取从联系人创建vCard数据所需的所有联系人密钥。
https://developer.apple.com/reference/contacts/cncontactvcardserialization
示例:
let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch:[CNContactVCardSerialization.descriptorForRequiredKeys()])