我不确定我这样做是否正确,我正在尝试将 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
与我的预期结果相比会这样。
提前致谢,供引用,提示如下:
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.
最佳答案
您需要实现 CustomStringConvertible
并实现 description
属性以返回您的 SwimmingWorkout
结构的字符串表示形式。
此外,由于您的预期输出将一个数字与每次游泳相关联(swim1
、swim2
),您可能还应该添加一个 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))"
}
}
关于arrays - 在 Swift 中将实例变量的属性保存到结构的静态变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50074361/