2015-12-19 15 views
5

Mam protokół i odpowiadający mu przedłużenie, który wygląda mniej więcej tak:Jak zrobić ograniczenie wewnętrzny protokół do dwóch typów w 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) 
     } 
    } 
} 

tak, to w losowej kontrolera widoku, który potwierdza ten protokół, ja robie tak:

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

działa to dobrze, ale byłoby to bardzo miłe, jeśli są dostępne tylko dla typów, które potwierdzić UIViewController I HelpViewControllerDelegate rozszerzenie HelpActionManageable metody.

coś takiego:

extension HelpActionManageable where Self == ItemType 

To nie działa. Jak mogę to osiągnąć w Swift?

+0

Nie sądzę, aby Swift już to wspierał. [Zobacz] (http://stackoverflow.com/questions/31187540/swift-generics-and-protocols-not-working-on-uikit-possible-bug) –

Odpowiedz

2

Myślę, że jest to teraz obsługiwane.

extension HelpActionManageable where Self == ItemType { 
} 

lub

extension HelpActionManageable where Self: ItemType { 
} 

pracuje dla mnie.

Powiązane problemy