简体   繁体   English

Swift:不能使用采用 Identifiable 的协议

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

I'm struggling to get this piece of code to work correctly:我正在努力让这段代码正常工作:

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

I'm facing the famous error: Protocol 'FileModelType' can only be used as a generic constraint because it has Self or associated type requirements我面临着著名的错误: Protocol 'FileModelType' can only be used as a generic constraint because it has Self or associated type requirements

I need to define an array of FileModelType objects but don't know how to do it in this context?我需要定义一个FileModelType对象数组,但不知道在这种情况下如何做?

I need to define an array of FileModelType objects but don't know how to do it in this context?我需要定义一个 FileModelType 对象数组,但不知道在这种情况下如何做?

It does not relate to the context.它与上下文无关。 The swift language does NOT allow this. swift 语言不允许这样做。

You can use您可以使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM