将实例变量的属性保存到Swift中的Struct的静态变量数组中

时间:2018-04-28 08:10:55

标签: arrays swift struct save

我不确定我是否正确地执行此操作,我试图将Struc的实例保存到静态变量数组,但实例的名称不是添加到数组中......以下是代码:

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")
        }
    }
}

我期待

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

应该导致输出:

swim1(distance: 100.0, time: 180.0, stroke: Stroke.butterfly)
swim2(distance: 200.0, time: 320.0, stroke: Stroke.butterfly)

但我明白了:

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)

我在这里缺少什么?我甚至不能完全确定为什么stroke: __lldb_expr_18.SwimmingWorkout.Stroke.butterfly与我预期的结果相比出现{。}}。

提前感谢,参考,提示:

  

向SwimmingWorkout添加一个名为save()的实例方法,该方法不带参数且没有返回值。此方法将其实例添加到SwimmingWorkout上与其游泳笔划相对应的静态数组。在save()里面写一个switch语句,它打开实例的stroke属性,并将self追加到正确的数组中。在上面创建的两个SwimmingWorkout实例上调用save,然后打印应该添加它们的数组,看看你的save方法是否正常工作。

2 个答案:

答案 0 :(得分:0)

删除

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

来自struct SwimmingWorkout并将其写出“struct”,您的代码应该是正确的。

答案 1 :(得分:0)

您需要实现CustomStringConvertible并实现description属性以返回SwimmingWorkout结构的字符串表示形式。

此外,由于您的预期输出会将每次游泳的数字与swim1swim2相关联,因此您也应该添加id属性。

所以,这是您更正的SwimmingWorkout结构:

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))"
    }
}