gpt4 book ai didi

ios - 局部变量声明 swift

转载 作者:行者123 更新时间:2023-11-28 15:51:08 25 4
gpt4 key购买 nike

我试图允许用户在我的应用程序中发送电子邮件,但我收到:

Use of local variable 'configureMailComposeViewController' before its declaration

这是代码。

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

最佳答案

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 {

}
}

关于ios - 局部变量声明 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42288598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com