You can write an extension to conform an Array
to Identifiable
.
Since extensions can't contain stored properties, and also because it makes sense that two arrays that are the "same" to also have the same id
, you'd need to compute the id
based on the contents of an array.
The simplest approach here is if you can conform your type to Hashable
:
extension MyType: Hashable {}
This also makes [MyType]
conform to Hashable
, and since id
could be any Hashable
, you could use the array itself as its own id
:
extension Array: Identifiable where Element: Hashable {
public var id: Self { self }
}
Or, if you want, the id
could be an Int
:
extension Array: Identifiable where Element: Hashable {
public var id: Int { self.hashValue }
}
Of course, you can do this just for your own type where Element == MyType
, but that type would need to be public
.