I'm unsure if I'm doing this correctly, I'm trying to save an instance of a Struc to a static variable array, but the name of the instance isn't being added to the array... Here's the code below:
struct SwimmingWorkout {
let distance: Double //meters
let time: Double //Seconds
let stroke: Stroke
enum Stroke {
case freestyle, butterfly, backstroke, breaststroke
}
static var freestyleWorkouts = [SwimmingWorkout]()
static var butterflyWorkouts = [SwimmingWorkout]()
static var backstrokeWorkouts = [SwimmingWorkout]()
static var breaststrokeWorkouts = [SwimmingWorkout]()
func save() {
switch stroke {
case .freestyle:
SwimmingWorkout.freestyleWorkouts += [self]
case .butterfly:
SwimmingWorkout.butterflyWorkouts += [self]
case .backstroke:
SwimmingWorkout.backstrokeWorkouts += [self]
case .breaststroke:
SwimmingWorkout.breaststrokeWorkouts += [self]
default:
print("Error")
}
}
}
I'm expecting that
let swim1 = SwimmingWorkout(distance: 100, time: 180, stroke: .butterfly)
let swim2 = SwimmingWorkout(distance: 200, time: 320, stroke: .butterfly)
swim1.save()
swim2.save()
for workout in SwimmingWorkout.butterflyWorkouts {
print(workout)
}
should result in output:
swim1(distance: 100.0, time: 180.0, stroke: Stroke.butterfly)
swim2(distance: 200.0, time: 320.0, stroke: Stroke.butterfly)
but I get:
SwimmingWorkout(distance: 100.0, time: 180.0, stroke: __lldb_expr_18.SwimmingWorkout.Stroke.butterfly)
SwimmingWorkout(distance: 200.0, time: 320.0, stroke: __lldb_expr_18.SwimmingWorkout.Stroke.butterfly)
What am I missing here? I'm not even entirely sure why stroke: __lldb_expr_18.SwimmingWorkout.Stroke.butterfly comes out that way it does compared to my expected result.
Thank in advance, for reference, here's the prompt:
Add an instance method to SwimmingWorkout called save() that takes no parameters and has no return value. This method will add its instance to the static array on SwimmingWorkout that corresponds to its swimming stroke. Inside save() write a switch statement that switches on the instance's stroke property, and appends self to the proper array. Call save on the two instances of SwimmingWorkout that you created above, and then print the array(s) to which they should have been added to see if your save method works properly.