更新到Swift 2.0时的代码问题. “无法转换值." [英] Problems with code when updating to Swift 2.0. "Cannot convert value.."
问题描述
我已经将Swift升级到2.0并自动转换了代码,但是我看到了一些我想修复的错误.
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.
其中之一是:无法将类型'[AnyObject]'的值转换为预期的参数类型'[String]?
One of them is : "cannot convert value of type '[AnyObject]' to expected argument type '[String]?
创建此VC的目的是启动电子邮件屏幕,以帮助用户与我联系.
This VC is created to launch an e mail screen in order to help users contact to me.
错误在func lanzarEmail的以下行中:
Error is in the following line in func lanzarEmail :
mailController?.setToRecipients(收件人)
mailController?.setToRecipients(recipients)
代码:
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"
如果它是恒定值.
try change line var destinatario: AnyObject!
to var destinatario: String!
. Or to let destinatario = "1234@hotmail.es"
if it is constant value.
mailController的方法setToReceipents
签名看起来像func setToRecipients(recipients: [String])
(实际上,这是属性var recipients: [String]
).但是您传递的是AnyObject
的数组,而不是String
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
这篇关于更新到Swift 2.0时的代码问题. “无法转换值."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!