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