[英]How to make a protocol extension constraint to two types in Swift
I have a protocol and its corresponding extension that looks something like this: 我有一个协议及其相应的扩展,看起来像这样:
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)
}
}
}
So, in a random view controller that confirms to this protocol, I am doing something like this: 所以,在随机视图控制器中确认了这个协议,我做的是这样的:
class RandomViewController : UIViewController, HelpViewControllerDelegate,HelpActionManageable {
//HelpViewControllerDelegate methods...
var viewController : RandomViewController {
return self
}
}
This works fine, but it would be very neat if the extension HelpActionManageable
methods are available for only types that confirm to UIViewController
AND HelpViewControllerDelegate
. 这样可以正常工作,但如果扩展
HelpActionManageable
方法仅适用于确认UIViewController
和HelpViewControllerDelegate
类型,那么它将非常简洁。
Something like this: 像这样的东西:
extension HelpActionManageable where Self == ItemType
This doesn't work. 这不起作用。 How can I achieve this in Swift?
我怎样才能在Swift中实现这一目标?
I think this is supported now. 我认为现在支持这一点。
extension HelpActionManageable where Self == ItemType {
}
or 要么
extension HelpActionManageable where Self: ItemType {
}
works for me. 适合我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.