簡體   English   中英

Swift:不能使用采用 Identifiable 的協議

[英]Swift: can't use a protocol that adopts Identifiable

我正在努力讓這段代碼正常工作:

protocol FileModelType: Identifiable {
    var id: ID { get set }
    var content: Data? { get set }
}

class Test: FileModelType {
    var id: Int = 0
    var content: Data? = nil
}

func test() {
    var models: [FileModelType] = [Test]()
}

我面臨着著名的錯誤: Protocol 'FileModelType' can only be used as a generic constraint because it has Self or associated type requirements

我需要定義一個FileModelType對象數組,但不知道在這種情況下如何做?

我需要定義一個 FileModelType 對象數組,但不知道在這種情況下如何做?

它與上下文無關。 swift 語言不允許這樣做。

您可以使用

func test() {
    var models = [Test]()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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