【发布时间】:2020-09-28 01:53:33
【问题描述】:
我的目标是使用一个按钮(包含多条消息)触发一个文本(制作标记,例如第一次单击将是方法1,第二次单击将是方法2)在末尾相应添加我的数据(加入后(分隔符:“~”)),这样当我回顾数据时,它可以帮助我分析点击了哪个按钮。
目前,我有一个可以输出数据的结构:
struct CaptureData {
var vertices: [SIMD3<Float>] //A vector of three scalar values. It will return a list of [SIMD3<Float>(x,y,z)]
var mode: Mode = .one
mutating func nextCase() { // the data method will be changed
mode = mode.next()
}
var verticesFormatted : String { //I formatted in such a way so that it can be read more clearly without SIMD3
let v = "<" + vertices.map{ "\($0.x):\($0.y):\($0.z)" }.joined(separator: "~") + "trial: \(mode.next().rawValue)"
return "\(v)"
}
}
基于@Joshua 的建议
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
func next() -> Self {
return self.nextCase
}
}
按钮在每次点击后交替显示消息,
var x = 0
var instance = CaptureData(vertices: [SIMD3<Float>])
// 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"
instance.nextCase()
}
else{
Textfield.text = "changing to cycling"
instance.nextCase()
}
x += 1
}
更新:我可以在分隔符“~”之后打印其中一种方法 .two(方法二)。但是,目前我仍然无法点击按钮切换数据中的大小写。
主要问题是变量的初始化。我无法定义var instance = CaptureData(vertices: [SIMD3<Float>]),因为它带有错误:Cannot convert value of type '[SIMD3<Float>].Type' to expected argument type '[SIMD3<Float>]'
如果我的解释有点混乱,我很抱歉。我试图描述我在这里遇到的问题。让我知道是否缺少任何东西!非常感谢您。
【问题讨论】:
-
突变不应该发生在结构而不是枚举中吗?
-
感谢您的回复!我对 swift 有点陌生,目前正在努力学习。你的意思是在结构中改变模式或 v 吗?你能指导我一下吗?