swift protocol, IBOutlet property cannot have non-object type

浪尽此生 提交于 2019-12-18 03:10:13

问题


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 ThumbnailTableViewCellDelegate {
    func cellWasTouched(thumbnail: Bool, cell: UITableViewCell)
}

class ThumbnailTableViewCell: UITableViewCell {
    @IBOutlet var thumbnailTableViewCellDelegate: ThumbnailTableViewCellDelegate?
}

unfortunately, the compiler complains with:

error: 'IBOutlet' property cannot have non-object type 'ThumbnailTableViewCellDelegate'
    @IBOutlet var thumbnailTableViewCellDelegate: ThumbnailTableViewCellDelegate?
    ^~~~~~~~~

回答1:


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.




回答2:


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:

  1. Declare the delegate as an AnyObject - @IBOutlet var delegate:AnyObject!
  2. Connect the delegate in Interface Builder
  3. Change the outlet's type to your protocol, e.g. @IBOutlet var delegate:MyDelegate

This works for me.




回答3:


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




回答4:


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.




回答5:


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))
}



回答6:


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.



来源:https://stackoverflow.com/questions/24561490/swift-protocol-iboutlet-property-cannot-have-non-object-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!