Identifiable Protocol in Swift: Class VS Struct

Identifiable protocol in Swift: class vs struct

As the documentation of Identifiable states, it does provide a default implementation for id for class types. However, there is no default implementation for structs, hence you need to add the property manually.

struct Test: Identifiable vs class Test: Identifiable

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
}
}

Identifiable id not required by reference types (class) but required by structs

Not exactly, there is an explicit extension for Identifiable, which makes that possible for classes:

demo
demo1

SwiftUI - Custom Identifier with Identifiable Protocol

You can use any Hashable as an id property required by Identifiable:

extension MyUserData: Identifiable {
var id: String { userId }
}

Identifiable protocol in swiftUI... id property... var vs let

Apparently not. You can safely turn the id into a let and all will work fine.

In fact, the Identifiable protocol has no conflict there. It only requires id to be gettable. It says nothing about settable:

public protocol Identifiable {

/// A type representing the stable identity of the entity associated with `self`.
associatedtype ID : Hashable

/// The stable identity of the entity associated with `self`.
var id: Self.ID { get }
}

Swift - struct type does not conform to protocol

Store has a property that is an array of Category. I would guess that Category does not conform to Equatable or Hashable, and so Swift cannot synthesize conformance.

Landmark contains properties that all conform to Equatable and Hashable, so it can synthesize conformance for Landmark.

There are two solutions to make Store conform.

  1. Make Category conform to both protocols. Then Swift could synthesize conformance for Store.

  2. Explicitly implement the conformance for Hashable and Equatable for Store by implementing static func == (lhs: Store, rhs: Store) -> Bool and func hash(into: inout Hasher)

Depending on the implementation of Category, the simplest solution just might be:

extension Category: Hashable, Equatable { }

Swift Conform to Identifiable with existing property

Use a computed property to return an existing property as the ID:

struct Event: Codable, Identifiable {
let eventID: String
//...other properties

var id: String { eventID } //or whatever
}

How to declare a specific protocol in Structure in Swift?

You may use generics, if that works for you:

struct Foo<T: View>: Identifiable {
var id = UUID()
var someView: T
}


Related Topics



Leave a reply



Submit