简体   繁体   English

局部变量声明迅速

[英]Local variable declaration swift

I'm trying to allow for a user to send an email in my app but I get a: 我正在尝试允许用户在我的应用程序中发送电子邮件,但收到以下消息:

Use of local variable 'configureMailComposeViewController' before its declaration 在声明之前使用局部变量“ configureMailComposeViewController”

Here is the code. 这是代码。

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
}

错误图片

func configuredMailComposeViewController() -> MFMailComposeViewController

This right here is your function that has a return type MFMailComposeViewController which you will get when you will call this method.It will return you some result 这是您的函数,其返回类型为MFMailComposeViewController ,当您调用此方法时将获得此类型,它将返回一些结果

Now the result from your above method func configuredMailComposeViewController() -> MFMailComposeViewControllercall need to be saved in a variable you just can not leave it to fly in the air .In order to save or get what ever this method does you need to do this -: 现在,上述方法func configuredMailComposeViewController() -> MFMailComposeViewControllercall方法的结果需要保存在一个变量中,只是不能让它飞散。为了保存或获取此方法需要执行的操作- :

var mailComposeViewController = configuredMailComposeViewController()

So this makes it clear you need to call this mailComposeViewController = configuredMailComposeViewController() after your return function not before. 因此,这很清楚,您需要在返回函数之后而不是之前调用此mailComposeViewController = configuredMailComposeViewController()

Exactly this-: 正是这样-

    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 {

        }
    }

Switch the order of the functions , put configuredMailComposeViewController function on top 切换功能的顺序,在顶部放置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 {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM