Hello I want to build something like below:
protocol BaseEntity: Identifiable {
var id: UUID { get set }
}
protocol DataStore {
associatedtype T: BaseEntity
typealias CompleteHandler<T> = ((Result<T, Error>) -> Void)
func read(with id: T.ID, completion: CompleteHandler<T>)
}
but in concrete implementation I want to make use of the UUID
that was defined in the BaseEntity
protocol:
class Storage<Entity: BaseEntity>: DataStore {
func read(with id: Entity.ID, completion: CompleteHandler<Entity>) {
// here, how to use that id as a UUID?
}
}
I don't know how to do it really, to define that there must be an object conforming to BaseEntity
and would like to use that UUID
id
property in the concrete implementation.
I don't want to do it like:
func read(with id: Entity.ID, completion: CompleteHandler<Entity>) {
guard let uuid = id as? UUID else {
return
}
// more code goes here...
}
How I should do it?