I would like to wire up a custom swift delegate in IB. The delegate is an object that implements a certain protocol in swift.
protocol ThumbnailTableViewCel
It kind of makes sense that IB requires AnyObject, rather than your particular protocol. The object you want to connect to probably, but not necessarily conforms to the protocol, and the protocol may have optionals - so:
Make your protocol like this:
@objc public protocol HexViewDataSource: NSObjectProtocol {
@objc optional func dataAtOffset (_ hexView: HexView, offset: UInt64, length: Int)-> Data?
@objc optional func dataLength (_ hexView: HexView) -> UInt64
}
Declare it in your class like this, for instance:
@IBOutlet weak open var dataSource: AnyObject?
And when you come to use it, check that it conforms to the protocol and that the optionals exist - like this:
if let dataSource = dataSource as? HexViewDataSource, let dfr = dataSource.dataAtOffset {
setRowData(offset: offset, data: dfr (self, offset, bytesPerRow))
}
You have to declare your ThumbnailTableViewCellDelegate
protocol as @objc
:
@objc protocol ThumbnailTableViewCellDelegate {
func cellWasTouched(thumbnail: Bool, cell: UITableViewCell)
}
This is because @IBOutlet
declares the variable as weak
, which only works with objects. I'm not sure why you can't just say the protocol conforms to AnyObject
, perhaps that's a Swift bug.
You can in connect your own protocols in IB with this workaround. It's a known issue with Xcode, so will probably be solved one day. Until then:
This works for me.
Not ideal, but an option is to do something like this:
@IBOutlet var objectType: NSObject!
private var conformingObject: SomeProtocol {
return objectType as SomeProtocol
}
Got to make sure your objectType
conforms to SomeProtocol
or things will explode
A variable of protocol type might not be an object, because structs and enums can conform to protocols too. To ensure that a protocol can only be conformed to by classes, you can declare the protocol with @class_protocol
.
IBOutlets are to indicate a pointer to an object stored in a nib (or storyboard) file. A protocol is not an object, therefore you can't have one in a nib file. Make the type of the IBOutlet var to be the type of the actual object you have in the nib.