gpt4 book ai didi

ios - Swift:使用参数作为同一函数中不同参数的默认值

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

我正在尝试定义一个函数来呈现邮件编辑器。它以委托(delegate)和演示者 UIViewController 作为参数(并不总是相同)但我不想为演示者和委托(delegate)者相同的情况提供默认值 UIViewController。那可能吗?

internal func showMailComposer(presenterController: UIViewController, delegate: UIViewController = presenterController, recipient: String = "", subject: String = "", body: BodyMessage = BodyMessage(string: "", isHTML: false)) {

guard let delegate = delegate as? MFMailComposeViewControllerDelegate else { return }

// ...
}

最佳答案

delegate 设为 Optional 并使用默认的 nil 然后使用 ?? 将其解包或替换为presenterController 如果没有提供:

internal func showMailComposer(presenterController: UIViewController, delegate: UIViewController? = nil, recipient: String = "", subject: String = "", body: BodyMessage = BodyMessage(string: "", isHTML: false)) {

guard let delegate = (delegate ?? presenterController) as? MFMailComposeViewControllerDelegate else { return }

// ...
}

关于ios - Swift:使用参数作为同一函数中不同参数的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53630912/

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