簡體   English   中英

類不符合協議

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM