-3

嘗試從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

回答

2

一個聯繫人可以有多個電話號碼所以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

相關問題