gpt4 book ai didi

ios - swift 如何传递 UIViewController 类型的参数

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:26 24 4
gpt4 key购买 nike

我有这个功能来呈现登录/注册模态视图 Controller 。传递的参数必须是 UIViewController<AuthViewControllerDelegate> (UIViewController 代表方法,AuthViewControllerDelegate 代表方法)

static func checkAuthError(controller: UIViewController<AuthViewControllerDelegate>, err: NSError) {
if err.code == 401 {
let authViewController = viewControllerWithIdentifier("AuthViewController") as! AuthViewController
authViewController.delegate = controller
controller.presentViewController(authViewController, animated: true, completion: nil)
}
}

但我无法传递 objective c 样式类型。我是否必须通过不同类型的同一 Controller 两次?

最佳答案

您可以为方法参数选择类或协议(protocol)类型,并有条件地将其转换为另一个:

func checkAuthError(controller: UIViewController, err: NSError) {
...
if authDelegate = controller as? AuthViewControllerDelegate {
...
}
}

或者,您可以使用泛型类型约束:

func checkAuthError<T: UIViewController where T: AuthViewControllerDelegate>(controller: T, err: NSError) {
...
}

关于ios - swift 如何传递 UIViewController<XXXDelegate> 类型的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702008/

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