How to make a protocol extension restriction for two types in Swift

I have a protocol and its corresponding extension, which 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 the arbitrary view controller that confirms 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 HelpActionManageable extension HelpActionManageable were only available for types that confirm the value of UIViewController AND HelpViewControllerDelegate .

Something like that:

 extension HelpActionManageable where Self == ItemType 

This does not work. How can I achieve this in Swift?

+5
source share
1 answer

I think this is supported now.

 extension HelpActionManageable where Self == ItemType { } 

or

 extension HelpActionManageable where Self: ItemType { } 

works for me.

+2
source

Source: https://habr.com/ru/post/1238693/


All Articles