[swift-evolution] Specify type of a delegate which conforms to a protocol

Alexey Demedetskiy alexey.demedetskiy at teamaol.com
Thu Feb 11 16:19:34 CST 2016


As alternative to a protocol solution, you can use value type wrapper:

struct Delegate<T where T:UIViewController, T:UITableViewDelegate> {
    let value: T
}

class MyController : UIViewController, UITableViewDelegate {}
class WrongController : UIViewController {}

let delegate = Delegate(value: MyController())

//  error: generic parameter 'T' could not be inferred
let delegate = Delegate(value: WrongController())

- Alexey Demedetskiy

> Hi All,
> I used to do this in Obj-C but I'm unable to do this in swift
> 
> *UIViewController<MyProtocol>*delegate;*
> 
> I posted this question on dev forums but didn't get the solution and
> finally I'm reporting here so that It can be added as a features in the
> coming releases.
> 
> Thanks
> 
> Rathore
> 
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160212/3daca5e7/attachment.html>


More information about the swift-evolution mailing list