簡體   English   中英

如何在Swift中對兩種類型進行協議擴展約束

[英]How to make a protocol extension constraint to two types in Swift

我有一個協議及其相應的擴展,看起來像這樣:

import Foundation


protocol HelpActionManageable {
    typealias ItemType : UIViewController,HelpViewControllerDelegate
    var viewController : ItemType {
        get
    }
}

extension HelpActionManageable  {
    func presentHelpViewController() {
        let helpViewController = HelpViewController(nibName: HelpViewController.nibName(), bundle: nil)
        viewController.presentViewController(helpViewController, animated: true, completion:nil)
        helpViewController.delegate = viewController
    }
    func dismissSuccessfulHelpViewController(helpViewController:HelpViewController) {
        helpViewController.dismissViewControllerAnimated(true) { () -> Void  in
            self.viewController.showAlertControllerWithTitle(GlobalConstants.Strings.SUCCESS, message: GlobalConstants.Strings.VALUABLE_FEEDBACK, actions: [], dismissingActionTitle: GlobalConstants.Strings.OK, dismissBlock: nil)
        }
    }
}

所以,在隨機視圖控制器中確認了這個協議,我做的是這樣的:

class RandomViewController : UIViewController, HelpViewControllerDelegate,HelpActionManageable {
    //HelpViewControllerDelegate methods...
    var viewController : RandomViewController {
        return self
    }
}

這樣可以正常工作,但如果擴展HelpActionManageable方法僅適用於確認UIViewControllerHelpViewControllerDelegate類型,那么它將非常簡潔。

像這樣的東西:

extension HelpActionManageable where Self == ItemType

這不起作用。 我怎樣才能在Swift中實現這一目標?

我認為現在支持這一點。

extension HelpActionManageable where Self == ItemType  {
}

要么

extension HelpActionManageable where Self: ItemType  {
}

適合我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM