二元运算符“*”不能应用于 'SCNVector3' 和 'Double' 类型的操作数

Binary operator '*' cannot be applied to operands of type 'SCNVector3' and 'Double'

我需要将 SCNVector3 乘以 0.1 以获得新位置。当我尝试这样做时,出现以下错误。这在早期的 Xcode 版本中是有效的。我正在使用 Xcode 10.1 和 Swift 4 版本的编译器。我看到了同类问题的其他答案,但这里的数据类型不同。

Binary operator '*' cannot be applied to operands of type 'SCNVector3' and 'Double'

我使用的代码如下,

   guard let pointOfView = sceneView.pointOfView else { return }

    let mat = pointOfView.transform
    let dir = SCNVector3(-1 * mat.m31, -1 * mat.m32, -1 * mat.m33)

let currentPosition = pointOfView.position + (dir * 0.1) ------> Getting error here

    let projectedPlaneCenter = self.sceneView.projectPoint(currentPosition)
    zVal = Double(projectedPlaneCenter.z)

没有为操作数 SCNVector3Double 定义运算符 *

我猜 someVector * 0.1 是指将向量的每个分量乘以 0.1?

在那种情况下,您可以定义自己的 * 运算符:

// put this in the global scope
func *(lhs: SCNVector3, rhs: Double) -> SCNVector3 {
    return SCNVector3(lhs.x * CGFloat(rhs), lhs.y * CGFloat(rhs), lhs.z * CGFloat(rhs))
}

// usage
SCNVector3(1, 2, 3) * 0.1 // (0.1, 0.2, 0.3)

将其放入您的项目中,它应该会起作用。

    public static func * (lhs: SCNVector3, rhs: Double) -> SCNVector3 {
        return SCNVector3(lhs.x * .init(rhs), lhs.y * .init(rhs), lhs.z * .init(rhs))
    }

    public static func * (lhs: Double, rhs: SCNVector3) -> SCNVector3 {
        return rhs * lhs
    }
}