2015-12-19 63 views
5

我有一個協議及其相應的擴展,它看起來是這樣的:如何使協議擴展約束兩種類型的斯威夫特

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中實現這一點?

+0

我不認爲斯威夫特支持這一點呢。 [見](http://stackoverflow.com/questions/31187540/swift-generics-and-protocols-not-working-on-uikit-possible-bug) –

回答

2

我認爲這是現在支持。

extension HelpActionManageable where Self == ItemType { 
} 

extension HelpActionManageable where Self: ItemType { 
} 

爲我工作。