1

Anyone know if there's a way to have a parameter be of two types?

Such as for the function:

func email(from viewcontroller : (UIViewController, MFMailComposeViewControllerDelegate) {

}
Hamish
  • 78,605
  • 19
  • 187
  • 280
Kjell
  • 801
  • 7
  • 19
  • Are you looking for the `viewcontroller` parameter to accept types that both inherit from `UIViewController` and conform to `MFMailComposeViewControllerDelegate`? If so, see [Swift: type must implement protocol and be a subclass of given class](http://stackoverflow.com/q/25825988/2976878) – Hamish Feb 03 '17 at 21:51

2 Answers2

2

Genericize your function's definition:

func email<T>(from viewController : T) where T: UIViewController, T: MFMailComposeViewControllerDelegate {
    // ...
}
Code Different
  • 90,614
  • 16
  • 144
  • 163
2

In Swift 4

func email(from viewcontroller : UIViewController & MFMailComposeViewControllerDelegate) {
}

Elegant, short and intuitive.

Dark
  • 864
  • 9
  • 17
  • Unless you try with a `CGFloat & Int` -- _Non-protocol, non-class type 'CGFloat' cannot be used within a protocol-constrained type_ – ruffin Jun 03 '20 at 00:55