【问题标题】:Binary operator '*' cannot be applied to operands of type 'SCNVector3' and 'Double'二元运算符“*”不能应用于“SCNVector3”和“Double”类型的操作数
【发布时间】:2019-08-23 11:37:00
【问题描述】:

我需要将 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)

让 currentPosition = pointOfView.position + (dir * 0.1) ------> 此处出错

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

【问题讨论】:

    标签: ios xcode swift4 arkit


    【解决方案1】:

    没有为操作数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)
    

    【讨论】:

      【解决方案2】:

      把它放到你的项目中,它应该可以工作。

          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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        相关资源
        最近更新 更多