繁体   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