gpt4 book ai didi

ios - 将类型作为变量传递

转载 作者:行者123 更新时间:2023-11-30 11:50:22 25 4
gpt4 key购买 nike

如何在 Swift 4 中将类型作为变量传递?

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! MapViewController

我在一个可怕的 if 语句中有与上面类似的内容,我想转移到一个函数,但不确定如何传递“MapViewController”(或任何 Controller 作为变量)。

'“伪”代码'

function setVC(withViewController : Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! withViewController
}

最佳答案

您可以使用如下通用参数:

function setVC<T>(withViewController: T) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! T
}

关于ios - 将类型作为变量传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48383505/

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