formatting - CNContactFormatter 需要哪些键?

我正在尝试使用新的 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/

相关文章:

excel - VBA:将文本转换为数字

java - 在 java 的 simpledateformat 中强制 4 位数年份

c++ - 将 double 指数的可移植打印到 C++ iostreams

.net - 用逗号格式化数字,但保留小数

c# - string.Format 与 string.Join

python - 在 Python 中截断十进制值

java - 如何在android中格式化longs以始终显示两位数

excel - 如何在 Excel 单元格内填零

c# - 如何在 c# 中生成像 "1st November"这样的日期格式

objective-c - 格式化数字以显示逗号和/或美元符号