无法将类型'[String:Any Object]'转换为预期类型'String'

问题描述:

尝试从CNContacts中获取电话号码字符串。我拉起了一个联系人选择器视图控制器,当用户选择多个联系人时,我创建了一个消息组合视图控制器。我需要创建一个字符串数组作为消息组成视图控制器的收件人传递。错误来自于下面的行... contactsPhoneNumber.append(phoneNumber)无法将类型'[String:Any Object]'转换为预期类型'String'

func AddFriendTapped() { 
    let contactPickerViewController = CNContactPickerViewController() 
    contactPickerViewController.delegate = self 
    presentViewController(contactPickerViewController, animated: true, completion: nil) 
} 


func contactPicker(picker: CNContactPickerViewController,didSelectContacts contacts: [CNContact]) { 

    //check if phone can send texts, if so, continue 
    if !MFMessageComposeViewController.canSendText(){ 

     let composeVC = MFMessageComposeViewController() 
     composeVC.messageComposeDelegate = self 

     //must get phone number strings from CNContact 
     let phoneNumberKey = [CNContactPhoneNumbersKey] 
     for contact in contacts { 
      var phoneNumber = contact.dictionaryWithValuesForKeys(phoneNumberKey) 
      contactsPhoneNumber.append(phoneNumber) 
     } 

     composeVC.recipients = contactsPhoneNumber 
     composeVC.body = "Hi, test message" 

     // Present the view controller modally. 
     dismissViewControllerAnimated(true) { 
      self.presentViewController(composeVC, animated: true, completion: nil) 
     } 

    } 

} 

func messageComposeViewController(controller: MFMessageComposeViewController, 
            didFinishWithResult result: MessageComposeResult) { 
    // Check the result or perform other tasks. 

    // Dismiss the mail compose view controller. 
    controller.dismissViewControllerAnimated(true, completion: nil) 
} 
+0

在哪行会发生编译器错误? – luk2302

+0

'var phoneNumbers = contact.phoneNumbers',不需要'dictionaryWithValuesForKeys'。这也应该告诉你'phoneNumbers'是一个数组 – Sulthan

+0

错误消息说,要分配给'phoneNumber'的值是一个字典(***字典** WithValuesForKeys *暗示)而不是期望的字符串 – vadian

一个联系人可以有多个电话号码所以contact.phoneNumbers返回CNlabeledValue的数组。你需要两个循环,一个迭代其他所有联系人以迭代所有数字。然后,您必须提取CNPhoneNumber类型的电话号码,然后将其转换为字符串。

我在您的代码中做了一些更改。希望能帮助到你。 :)

func contactPicker(picker: CNContactPickerViewController,didSelectContacts contacts: [CNContact]) { 

    //check if phone can send texts, if so, continue 
    if !MFMessageComposeViewController.canSendText(){ 

     let composeVC = MFMessageComposeViewController() 
     composeVC.messageComposeDelegate = self 

     //must get phone number strings from CNContact 

     //let phoneNumberKey = [CNContactPhoneNumbersKey] 


     for contact in contacts { 
      let contactNumberArray = contact.phoneNumbers 
      for contactNumber in contactNumberArray{ 
       let number = contactNumber.value as! CNPhoneNumber 
       contactsPhoneNumber.append(number.stringValue) 
      } 
     } 


     composeVC.recipients = contactsPhoneNumber 
     composeVC.body = "Hi, test message" 

     // Present the view controller modally. 
     dismissViewControllerAnimated(true) { 
      self.presentViewController(composeVC, animated: true, completion: nil) 
     } 

    } 

} 
+0

字mayne,我想出来了,道具上的回应! – cnichs27