我正在使用新的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)) }