简体   繁体   English

swift如何传递UIViewController类型的参数<XXXDelegate>

[英]swift how to pass parameter of type UIViewController<XXXDelegate>

I have this function to present signin/signup modal view controller. 我有这个功能来呈现登录/注册模态视图控制器。 The parameter passed has to be UIViewController<AuthViewControllerDelegate> ( UIViewController for presenting method, AuthViewControllerDelegate for delegate method) 传递的参数必须是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)
    }
}

But I can't pass the objective c style type. 但我不能通过目标c样式类型。 Do I have to pass the same controller twice, with different types? 我是否必须使用不同类型的同一个控制器两次?

You could choose either the class or protocol type for the method argument and conditionally cast it to the other: 您可以为方法参数选择类或协议类型,并有条件地将其转换为另一个:

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

Alternatively, you could use a generic type constraint: 或者,您可以使用泛型类型约束:

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

It sounds like 这听起来像

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

will be deprecated soon, replaced by: 将很快弃用,取而代之的是:

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

For clarity, the where clause comes after any return values. 为清楚起见, where子句位于任何返回值之后。

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

相关问题 如何将UIViewController作为参数传递给swift 3? - How to pass UIViewController as a parameter swift 3? 如何将一个UIViewController类型传递给一个函数并在iOS Swift中输入一个变量? - How to pass a Type of UIViewController to a function and type cast a variable in iOS Swift? 如何使用 swift 将 UIVIewController 名称作为参数传递给特定函数? - How to pass UIVIewController name as a parameter to particular function using swift? 如何在Swift中传递Type作为参数 - How to pass a Type as a parameter in Swift 如何在Swift中传递“T”类型的“序列”作为参数? - How to pass a “Sequence” of type “T” as a parameter in Swift? 如何将UIViewController传递给Swift中的另一个类? - How to pass UIViewController to another class in Swift? 如何在 Swift 中的 UIViewController 和 NSObject 之间传递数据 - How to pass data between a UIViewController and an NSObject in Swift 如何在Swift中将数据从UITableViewRowAction传递到UIViewController? - How to pass data to a UIViewController from a UITableViewRowAction in Swift? iOS Swift4如何协调T.Type和type(of :)传递动态类类型作为函数参数? - iOS Swift4 how to reconcile T.Type and type(of:) to pass dynamic class type as function parameter? 如何将数据传递到 UIView 一侧的嵌入式 UIViewController - Swift 5 - How to pass data to a embedded UIViewController in side a UIView - Swift 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM