Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过CNContactViewController显示时,Contact缺少一些必需的关键描述符_Ios_Iphone_Xcode_Swift - Fatal编程技术网

Ios 通过CNContactViewController显示时,Contact缺少一些必需的关键描述符

Ios 通过CNContactViewController显示时,Contact缺少一些必需的关键描述符,ios,iphone,xcode,swift,Ios,Iphone,Xcode,Swift,试图在给定的tableView中显示带有预构建UI的联系人,当用户选择要显示的联系人时,出现以下错误: CNPropertyNotFetchedException',原因:'联系人0x7fded8ee6f40为 缺少一些必需的密钥描述符:[CNContactViewController] 描述符或所需密钥]> 我已经尝试过用这种方法来解决: 因此,我的联系人数组创建如下所示: func searchContactDataBaseOnName(name: String) { r

试图在给定的tableView中显示带有预构建UI的联系人,当用户选择要显示的联系人时,出现以下错误:

CNPropertyNotFetchedException',原因:'联系人0x7fded8ee6f40为 缺少一些必需的密钥描述符:[CNContactViewController] 描述符或所需密钥]>

我已经尝试过用这种方法来解决:

因此,我的联系人数组创建如下所示:

  func searchContactDataBaseOnName(name: String) {
        results.removeAll()

        let predicate = CNContact.predicateForContactsMatchingName(name)
        //Fetch Contacts Information like givenName and familyName
        let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactViewController.descriptorForRequiredKeys()]

        let store = CNContactStore()        
        do {
            let contacts = try store.unifiedContactsMatchingPredicate(predicate,
                keysToFetch: keysToFetch)                
            for contact in contacts {
                self.results.append(contact)
            }
            tableContacts.reloadData()
        }
        catch{
            print("Can't Search Contact Data")
        }
    }
当用户点击行索引时,我尝试通过以下方式显示:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

        let viewControllerforContact = CNContactViewController(forContact: results[indexPath.row])
        viewControllerforContact.contactStore = self.contactStore
        viewControllerforContact.delegate = self

        self.navigationController?.pushViewController(viewControllerforContact,animated:true)
    }
有什么解决办法吗?似乎我仍然无法将描述符orrequiredKeys传递给数组“Results”。。。可能吧?

您正在获取所需的密钥,并将它们存储在从中调用的结果变量中,因此我不知道原因。您可以使用所需的密钥重新获取联系人,以避免错误:

var contact = results[indexPath.row]    
if !contact.areKeysAvailable([CNContactViewController.descriptorForRequiredKeys()]) {
    do {
        contact = try self.contactStore.unifiedContactWithIdentifier(contact.identifier, keysToFetch: [CNContactViewController.descriptorForRequiredKeys()])
    }
    catch { }
}
let viewControllerforContact = CNContactViewController(forContact: contact)