我正在尝试使用新的 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给出任何线索。
谢谢!
最佳答案
我在 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))
}
https://stackoverflow.com/questions/33384044/