更新到 Swift 2.0 时出现代码问题。 "Cannot convert value.."

Problems with code when updating to Swift 2.0. "Cannot convert value.."

我已将 Swift 更新为 2.0 并自动转换了代码,但我发现了一些我想修复的错误。

其中之一是:“无法将‘[AnyObject]’类型的值转换为预期的参数类型‘[String]?

创建此 VC 是为了启动电子邮件屏幕,以帮助用户与我联系。

错误出现在 func lanzarEmail 的以下行中:

mailController?.setToRecipients(收件人)

代码:

import UIKit
import MessageUI

class tuPropiaHistoriaVC: UIViewController,MFMailComposeViewControllerDelegate {

var alert: UIAlertView?
var subjectText:String?
var destinatario:AnyObject!
var mailController:MFMailComposeViewController?

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    //Recipients
    subjectText = "Mi historia de éxito / superación."
    destinatario = "1234@hotmail.es"

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func lanzarEmail(sender: AnyObject) {

    if(MFMailComposeViewController.canSendMail()){


        //AlertView
        alert = UIAlertView()
        alert!.addButtonWithTitle("Ok")

        mailController = MFMailComposeViewController()
        //asignar delegado al controlador de email
        mailController?.mailComposeDelegate = self




        //Completar objeto mailController

        mailController?.setSubject(subjectText!)

        var recipients = [destinatario]

        mailController?.setToRecipients(recipients)
        self.presentViewController(mailController!, animated: true, completion: nil)

    }else
    {
        let sendMailErrorAlert = UIAlertView(title: "No se puede enviar Email", message: "Por favor configura tu app Mail para poder enviar correos", delegate: self, cancelButtonTitle: "Entendido")
        sendMailErrorAlert.show()
        self.dismissViewControllerAnimated(false, completion: nil)

    }






}


func  mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {


    switch result.rawValue {

    case MFMailComposeResultCancelled.rawValue:
        //se cancelo envio
        alert!.title = "Envio cancelado"
        alert!.message = "Se canceló el envio"
        alert!.show()

    case MFMailComposeResultSaved.rawValue:
        //se guardo draft
        alert!.title = "Correo guardado"
        alert!.message = "Se guardó el correo en la app de Mail"
        alert!.show()

    case MFMailComposeResultFailed.rawValue:
        //fallo el envio
        alert!.title = "Error"
        alert!.message = "El correo no pudo ser enviado"
        alert!.show()

    case MFMailComposeResultSent.rawValue:
        //el mail se pudo enviar y esta en la pila de envio
        alert!.title = "Correo enviado"
        alert!.message = "El correo se envió exitosamente"
        alert!.show()

    default:
        break


    }


    mailController?.dismissViewControllerAnimated(true,

        //Clousure a ejecutar al finalizar de mostrar la vista

        completion: { () -> Void in


            //Cerrar pantalla del view base, requiere que la conexion entre pantallas sea del tipo seague
            self.dismissViewControllerAnimated(true, completion: nil)



    })



}



}

尝试将行 var destinatario: AnyObject! 更改为 var destinatario: String!。或者 let destinatario = "1234@hotmail.es" 如果它是常量值。

mailController 的方法 setToReceipents 签名看起来像 func setToRecipients(recipients: [String])(实际上,这是 属性 var recipients: [String])。但是你传递的 AnyObject 数组不是 String

数组