![](/img/trans.png)
[英]Swift protocol extension with constraint won't make class to conform that protocol
[英]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
方法僅適用於確認UIViewController
和HelpViewControllerDelegate
類型,那么它將非常簡潔。
像這樣的東西:
extension HelpActionManageable where Self == ItemType
這不起作用。 我怎樣才能在Swift中實現這一目標?
我認為現在支持這一點。
extension HelpActionManageable where Self == ItemType {
}
要么
extension HelpActionManageable where Self: ItemType {
}
適合我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.