我的目标是使用一个按钮(包含多条消息)来触发文本(将标记(例如,第一次单击将成为方法1,第二次单击将成为方法2))相应地添加到我的数据末尾(在加入(分隔符:“〜”)之后),这样可以帮助我分析在查看数据时单击了哪个按钮。
当前,我有一个将输出数据的结构:
struct CaptureData {
var vertices: [SIMD3<Float>]
var mode: Mode = .one
var verticesFormatted : String {
let v = "<" + vertices.map{ "\($0.x):\($0.y):\($0.z)" }.joined(separator: "~") + "method: \(mode.next().rawValue)" //I would like to add the marker here after the click, but there is an error here please see the description on the method I tried
return "\(v)"
}
}
[基于帖子Swift: one button to alternate multiple test message in UIView上的@Leo Dabus,
enum Mode: String, CaseIterable {
case one, two, three
}
extension CaseIterable where Self: Equatable {
var allCases: AllCases { Self.allCases }
var nextCase: Self {
let index = allCases.index(after: allCases.firstIndex(of: self)!)
guard index != allCases.endIndex else { return allCases.first! }
return allCases[index]
}
@discardableResult
mutating func next() -> Self {
self = nextCase
return self
}
}
我曾尝试使用CaseIterable
实现这一目标,但显然出现了错误(我相信mutation不适用于struct)
with Cannot use mutating member on immutable value: 'self' is immutable
我了解到CaseIterable将不适用于struct
并且按钮在每次单击后交替显示消息,
var x = 0
// Next button for changing methods
@IBAction func ChangingTapped(_ btn: UIButton) {
if(x==0){
Textfield.text = "changing to driving"
}
else if(x==1){
Textfield.text = "changing to walking"
}
else{
Textfield.text = "changing to cycling"
}
x += 1
}
如果我的解释在这里有些混乱,很抱歉。我试图描述我在这里遇到的问题。让我知道是否有任何遗漏!提前非常感谢。
Enums是一种数据类型,它更像是一个常量,但更具可读性。