简体   繁体   中英

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

I need to define an array of FileModelType objects but don't know how to do it in this context?

I need to define an array of FileModelType objects but don't know how to do it in this context?

It does not relate to the context. The swift language does NOT allow this.

You can use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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