Swift: 正确初始化 SIMD3 并从结构中调用它
Swift: Properly initialize SIMD3 and call it from struct
我正在尝试调用已定义的结构,以便在按下按钮时能够更改其值。我在结构内调用 SIMD3 时遇到问题。目前我有,
struct CaptureData {
var vertices: [SIMD3<Float>]
var mode: Mode = .one
mutating func nextCase() {
mode = mode.next()
}
var verticesFormatted : String {
let v = "<" + vertices.map{ "\([=11=].x):\([=11=].y):\([=11=].z)" }.joined(separator: "~") + "method: \(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 instance = CaptureData(vertices: [SIMD3<Float>])
,但出现错误:Cannot convert value of type '[SIMD3<Float>].Type' to expected argument type '[SIMD3<Float>]'
@Jousha 还建议我使用以下内容:
typealias XYZVar = (x: Float, y: Float, z: Float)
struct CaptureData {
var vertices:[XYZVar]
.... other variables
}
但是,我也用 var instance = CaptureData(vertices: [XYZVar])
尝试过,但它也不起作用:Cannot convert value of type '[XYZVar].Type' (aka 'Array<(x: Float, y: Float, z: Float)>.Type') to expected argument type '[XYZVar]' (aka 'Array<(x: Float, y: Float, z: Float)>')
我的问题是,如何正确调用 SIMD3 并修复来自 var instance = CaptureData(vertices: [SIMD3<Float>])
的上述错误?
非常感谢。
嗯,我从哪里开始?
您正在将类型 [SIMD3<Float>]
和 [XYZVar]
传递给需要值作为参数的 function/initializer。这就是编译器抱怨的原因。
让我解释一下。
在 swift 中,当你写 var vertices: [SIMD3<Float>]
时,你是在说:"vertices" 是 SIMD3<Float>
的数组。现在,如果你想给 "vertices" 赋值,你可以写 vertices = []
或 vertices = [SIMD3<Float>]()
它们是等价的。
我对您的代码做了一些修改。看看:
import simd
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
}
}
struct CaptureData {
var vertices: [SIMD3<Float>]
var mode: Mode = .one
mutating func nextCase() {
mode = mode.next()
}
var verticesFormatted : String {
let verticesDescribed = vertices
.map({ "\([=10=].x):\([=10=].y):\([=10=].z)" })
.joined(separator: "~")
let v = "< \(verticesDescribed) method: \(mode.next().rawValue)"
return v
}
}
let data = CaptureData(vertices: [
SIMD3<Float>(x: 0, y: 0, z: 0),
SIMD3<Float>(x: 0.5, y: 1, z: 0),
SIMD3<Float>(x: 1, y: 0, z: 0)
])
print(data.verticesFormatted)
// prints: < 0.0:0.0:0.0~0.5:1.0:0.0~1.0:0.0:0.0 method: two
我正在尝试调用已定义的结构,以便在按下按钮时能够更改其值。我在结构内调用 SIMD3 时遇到问题。目前我有,
struct CaptureData {
var vertices: [SIMD3<Float>]
var mode: Mode = .one
mutating func nextCase() {
mode = mode.next()
}
var verticesFormatted : String {
let v = "<" + vertices.map{ "\([=11=].x):\([=11=].y):\([=11=].z)" }.joined(separator: "~") + "method: \(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 instance = CaptureData(vertices: [SIMD3<Float>])
,但出现错误:Cannot convert value of type '[SIMD3<Float>].Type' to expected argument type '[SIMD3<Float>]'
@Jousha 还建议我使用以下内容:
typealias XYZVar = (x: Float, y: Float, z: Float)
struct CaptureData {
var vertices:[XYZVar]
.... other variables
}
但是,我也用 var instance = CaptureData(vertices: [XYZVar])
尝试过,但它也不起作用:Cannot convert value of type '[XYZVar].Type' (aka 'Array<(x: Float, y: Float, z: Float)>.Type') to expected argument type '[XYZVar]' (aka 'Array<(x: Float, y: Float, z: Float)>')
我的问题是,如何正确调用 SIMD3 并修复来自 var instance = CaptureData(vertices: [SIMD3<Float>])
的上述错误?
非常感谢。
嗯,我从哪里开始?
您正在将类型 [SIMD3<Float>]
和 [XYZVar]
传递给需要值作为参数的 function/initializer。这就是编译器抱怨的原因。
让我解释一下。
在 swift 中,当你写 var vertices: [SIMD3<Float>]
时,你是在说:"vertices" 是 SIMD3<Float>
的数组。现在,如果你想给 "vertices" 赋值,你可以写 vertices = []
或 vertices = [SIMD3<Float>]()
它们是等价的。
我对您的代码做了一些修改。看看:
import simd
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
}
}
struct CaptureData {
var vertices: [SIMD3<Float>]
var mode: Mode = .one
mutating func nextCase() {
mode = mode.next()
}
var verticesFormatted : String {
let verticesDescribed = vertices
.map({ "\([=10=].x):\([=10=].y):\([=10=].z)" })
.joined(separator: "~")
let v = "< \(verticesDescribed) method: \(mode.next().rawValue)"
return v
}
}
let data = CaptureData(vertices: [
SIMD3<Float>(x: 0, y: 0, z: 0),
SIMD3<Float>(x: 0.5, y: 1, z: 0),
SIMD3<Float>(x: 1, y: 0, z: 0)
])
print(data.verticesFormatted)
// prints: < 0.0:0.0:0.0~0.5:1.0:0.0~1.0:0.0:0.0 method: two