Roman
Roman

Reputation: 1498

struct Test: Identifiable vs class Test: Identifiable

struct Test: Identifiable {}

causes the error: "Type 'Test' does not conform to protocol 'Identifiable'".
It wants "id" property.

class Test: Identifiable {}

compiles without any problems.
Why?

Upvotes: 1

Views: 457

Answers (1)

Martin R
Martin R

Reputation: 539795

From SE-0261 Identifiable Protocol (emphasis mine):

In order to make it as convenient as possible to conform to Identifiable, a default id is provided for all class instances:

extension Identifiable where Self: AnyObject {
    var id: ObjectIdentifier {
        return ObjectIdentifier(self)
    }
}

Then, a class whose instances are identified by their object identities need not explicitly provide an id:

final class Contact: Identifiable {
    var name: String

    init(name: String) {
        self.name = name
    }
}

Upvotes: 3

Related Questions