I have three structs that are codable and identifiable: SetEntry
, TimeEntry
, and DistanceEntry
. I have another struct called WorkoutLog
which takes in an Exercise
and has a property entry
. What I am trying to do is allow entry to be any of the above entry types. I thought about using a protocol, but I couldn't get it decodable and I don't know how to fix that. I know that with protocols you have to use the any Protocol
syntax and I think that is what is limiting me from making it codable, but I can't really be sure. I also know that we could use enums with associated types but I don't know if this is good for production-level code. Any insights would be greatly appreciated.
struct SetEntry: Identifiable, Codable {
var id: UUID
var reps: Int
var weight: Double
init(id: UUID = UUID(), reps: Int, weight: Double) {
self.id = id
self.reps = reps
self.weight = weight
}
}
struct DistanceEntry: Codable, Identifiable {
var id: UUID
var distanceInMeters: Int
init(id: UUID = UUID(), distanceInMeters: Int) {
self.id = id
self.distanceInMeters = distanceInMeters
}
}
struct TimeEntry: Identifiable, Codable {
var id: UUID
var timeInSeconds: Int
init(id: UUID = UUID(), timeInSeconds: Int) {
self.id = id
self.timeInSeconds = timeInSeconds
}
}
struct WorkoutLog: Identifiable, Codable {
var id: UUID
var exercise: Exercise
var entry: ???
}