[英]Class does not conform to protocol
这是我的协议:
protocol LiveTableViewCellProtocol: class {
var data: LiveCellObjectProtocol! { get set }
}
这是我的课:
class RepliesTableViewCell: UITableViewCell, LiveTableViewCellProtocol {
var data: RepliesCellObject! //ERROR! does not conform to protocol.
}
RepliesCellObject
定义为:
public class RepliesCellObject: NSObject , LiveCellObjectProtocol{
//basic stuff here.
}
RepliesCellObject
是一个LiveCellObjectProtocol ...为什么我的表格单元格不符合要求?
它不符合要求,因为在符合LiveTableViewCellProtocol的对象中,您可以将数据设置为任何 LiveCellObjectProtocol,包括不是NSObject的对象。 在RepliesTableViewCell中,您不能这样做。 数据必须设置为LiveCellObjectProtocol,它也是一个NSObject。
因此,RepliesTableViewCell不符合LiveTableViewCellProtocol。
它必须与协议中明确规定的相同。 不允许执行您的操作,因为操作不完全相同。 请记住,您可以使用as!
如果确定的话,使其成为RepleisCellObject。
associatedtype
可以在这里提供帮助
protocol LiveTableViewCellProtocol: class {
associatedtype Data: LiveCellObjectProtocol
var data: Data! { get set }
}
您应该使用关联的类型
protocol LiveTableViewCellProtocol: class {
associatedtype Object : LiveCellObjectProtocol
var data: Object! { get set }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.