Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

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

I have updated Swift to 2.0 and converted the code automatically but I have seen some errors yet that I would like to fix.

One of them is : "cannot convert value of type '[AnyObject]' to expected argument type '[String]?

This VC is created to launch an e mail screen in order to help users contact to me.

Error is in the following line in func lanzarEmail :

mailController?.setToRecipients(recipients)

The code:

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 = "[email protected]"

    // 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)



    })



}



}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try change line var destinatario: AnyObject! to var destinatario: String!. Or to let destinatario = "[email protected]" if it is constant value.

mailController's method setToReceipents signature looks like func setToRecipients(recipients: [String]) (actually, this is property var recipients: [String]). But you pass array of AnyObject, which is not array of String


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...