在 Swift 中将实例变量的属性保存到 Struct 的静态变量数组中
Posted
技术标签:
【中文标题】在 Swift 中将实例变量的属性保存到 Struct 的静态变量数组中【英文标题】:Saving Properties of an Instance Variable to a Struct's Static Variable Array in Swift 【发布时间】:2018-04-28 08:10:54 【问题描述】:我不确定我是否正确执行此操作,我正在尝试将 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 方法是否正常工作。
【问题讨论】:
【参考方案1】:您需要实现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))"
【讨论】:
【参考方案2】:删除
enum Stroke
case freestyle, butterfly, backstroke, breaststroke
来自struct SwimmingWorkout
并将其写出“结构”,您的代码应该是正确的。
【讨论】:
以上是关于在 Swift 中将实例变量的属性保存到 Struct 的静态变量数组中的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift WKWebView 中的完成处理程序中将 html 数据保存在变量中
如何在 Swift 中将字符串数组保存到 Core Data
按下 UIButton 时,Swift 将文本保存到实例/全局变量