局部变量声明迅速

时间:2017-02-17 02:56:48

标签: ios swift swift3

我正在尝试允许用户在我的应用中发送电子邮件,但我得到了:

  

在声明

之前使用局部变量'configureMailComposeViewController'

这是代码。

func item(_ item: Int, selectedAtContactIndex index: Int) {
    var mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.presentViewController(mailComposeViewController, animated: true, completion: nil)
    } else {

    }
}

func configuredMailComposeViewController() -> MFMailComposeViewController{
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients([EmailAddressArray[index]])
    mailComposerVC.setSubject("I need your help")

    return mailComposerVC
}

Image of the Error

2 个答案:

答案 0 :(得分:1)

func configuredMailComposeViewController() -> MFMailComposeViewController

这就是你的函数,它有一个返回类型MFMailComposeViewController,当你调用这个方法时你会得到它。它会返回一些结果

现在上面的方法func configuredMailComposeViewController() -> MFMailComposeViewControllercall的结果需要保存在变量中,你不能让它在空中飞行。为了保存或得到你需要的方法这样做 - :

var mailComposeViewController = configuredMailComposeViewController()

因此,这清楚地表明您需要在返回功能之后再调用此mailComposeViewController = configuredMailComposeViewController()

正是这样 - :

    func configuredMailComposeViewController() -> MFMailComposeViewController{
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self

        mailComposerVC.setToRecipients([EmailAddressArray[index]])
        mailComposerVC.setSubject("I need your help")

        return mailComposerVC
    }





        func item(_ item: Int, selectedAtContactIndex index: Int) {

        if MFMailComposeViewController.canSendMail() {
var mailComposeViewController = configuredMailComposeViewController()
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {

        }
    }

答案 1 :(得分:0)

切换功能的顺序,将configuredMailComposeViewController功能置于顶部

    func configuredMailComposeViewController() -> MFMailComposeViewController{
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients([EmailAddressArray[index]])
    mailComposerVC.setSubject("I need your help")

    return mailComposerVC
}





    func item(_ item: Int, selectedAtContactIndex index: Int) {
    var mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.presentViewController(mailComposeViewController, animated: true, completion: nil)
    } else {

    }
}