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 struct
s, 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:
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.
Make
Category
conform to both protocols. Then Swift could synthesize conformance forStore
.Explicitly implement the conformance for
Hashable
andEquatable
forStore
by implementingstatic func == (lhs: Store, rhs: Store) -> Bool
andfunc 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
How to Load the Photo Library into Uicollectionview? Swift
Using Structs (Bytes) with Swift - Struct to Nsdata and Nsdata to Struct
Create CSV File in Swift and Write to File
How to Retrieve a Value from Dictionary in Swift 3
Swiftui - Pass Data to Different Views
Swift: How to Hold Any Possible Instance of a Generic Type in a Variable
How to Create a Nsmutabledictionary in Swift
Swift: Reusable Uiview in Storyboard and Sizing Constraints
How to Blur the Background in a Swiftui MACos Application
Identify Mkpointannotation in Mapview
Draw Mkpointannotation with Title in Mksnapshot Image
Protocol Extension Initializer Forcing to Call Self.Init
Implicit Cast Function Receiving Tuple
Setting Arkit Orientation via Swift
What Might Be Causing This Animation Bug with Swiftui and Navigationview
Swift Combine: Using Timer Publisher in an Observable Object