嘗試從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)
}
在哪行會發生編譯器錯誤? – luk2302
'var phoneNumbers = contact.phoneNumbers',不需要'dictionaryWithValuesForKeys'。這也應該告訴你'phoneNumbers'是一個數組 – Sulthan
錯誤消息說,要分配給'phoneNumber'的值是一個字典(***字典** WithValuesForKeys *暗示)而不是期望的字符串 – vadian