92
votes

In Objective-C, it's possible to specify a class conforming to a protocol as a method parameter. For example, I could have a method that only allows a UIViewController that conforms to UITableViewDataSource:

- (void)foo:(UIViewController<UITableViewDataSource> *)vc;

I can't find a way to do this in Swift (perhaps it's not possible yet). You can specify multiple protocols using func foo(obj: protocol<P1, P2>), but how do you require that the object is of a particular class as well?

8
You could make a custom class, for example MyViewControllerClass, and make sure that class conforms to the protocol you care about. Then declare the argument accepts that custom class. I realize it wouldn't work for every situation but, it's a way... not an answer to your question though. More of a workaround.CommaToast

8 Answers

135
votes

You can define foo as a generic function and use type constraints to require both a class and a protocol.

Swift 4

func foo<T: UIViewController & UITableViewDataSource>(vc: T) {
    .....
}

Swift 3 (works for Swift 4 also)

func foo<T: UIViewController>(vc:T) where T:UITableViewDataSource { 
    ....
}

Swift 2

func foo<T: UIViewController where T: UITableViewDataSource>(vc: T) {
    // access UIViewController property
    let view = vc.view
    // call UITableViewDataSource method
    let sections = vc.numberOfSectionsInTableView?(tableView)
}
29
votes

In Swift 4 you can achieve this with the new & sign:

let vc: UIViewController & UITableViewDataSource
17
votes

The Swift book documentation suggests that you use type constraints with a where clause:

func someFunction<C1: SomeClass where C1:SomeProtocol>(inParam: C1) {}

This guarantees that "inParam" is of type "SomeClass" with a condition that it also adheres to "SomeProtocol". You even have the power to specify multiple where clauses delimited by a comma:

func itemsMatch<C1: SomeProtocol, C2: SomeProtocol where C1.ItemType == C2.ItemType,    C1.ItemType: SomeOtherProtocol>(foo: C1, bar: C2) -> Bool { return true }
4
votes

With Swift 3, you can do the following:

func foo(_ dataSource: UITableViewDataSource) {
    self.tableView.dataSource = dataSource
}

func foo(_ delegateAndDataSource: UITableViewDelegate & UITableViewDataSource) { 
    //Whatever
}
4
votes

Swift 5:

func foo(vc: UIViewController & UITableViewDataSource) {
    ...
}

So essentially Jeroen's answer above.

2
votes

What about this way?:

protocol MyProtocol {
    func getTableViewDataSource() -> UITableViewDataSource
    func getViewController() -> UIViewController
}

class MyVC : UIViewController, UITableViewDataSource, MyProtocol {

    // ...

    func getTableViewDataSource() -> UITableViewDataSource {
        return self
    }

    func getViewController() -> UIViewController {
        return self
    }
}

func foo(_ vc:MyProtocol) {
    vc.getTableViewDataSource() // working with UITableViewDataSource stuff
    vc.getViewController() // working with UIViewController stuff
}
0
votes

Note in September 2015: This was an observation in the early days of Swift.

It seems to be impossible. Apple has this annoyance in some of their APIs as well. Here is one example from a newly introduced class in iOS 8 (as of beta 5):

UIInputViewController's textDocumentProxy property:

Defined in Objective-C as follows:

@property(nonatomic, readonly) NSObject<UITextDocumentProxy> *textDocumentProxy;

and in Swift:

var textDocumentProxy: NSObject! { get }

Link to Apple' documentation: https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIInputViewController_Class/index.html#//apple_ref/occ/instp/UIInputViewController/textDocumentProxy

0
votes

Update for Swift 5:

func yourFun<V: YourClass>(controller: V) where V: YourProtocol