1

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.

2 Answers 2

1

You need to implement CustomStringConvertible and implement the description property to return a string representation of your SwimmingWorkout struct.

Also, since your expected output associates a number with each swim (swim1, swim2), you should probably add an id property too.

So here's your corrected SwimmingWorkout struct:

struct SwimmingWorkout: CustomStringConvertible {
    let distance: Double //meters
    let time: Double //Seconds
    let stroke: Stroke

    static var nextID = 1
    let id: Int

    init(distance: Double, time: Double, stroke: Stroke) {
        self.distance = distance
        self.time = time
        self.stroke = stroke
        self.id = SwimmingWorkout.nextID
        SwimmingWorkout.nextID += 1
    }

    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]
        }
    }

    var description: String {
        return "swim\(id)(distance: \(distance), time: \(time), stroke: Stroke.\(stroke))"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Remove

enum Stroke {
        case freestyle, butterfly, backstroke, breaststroke
    }

from struct SwimmingWorkout and write it out of "struct" and your codes should be correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.