无法转换类型'(CMDeviceMotion?,NSError?) - >的值()'预期参数类型' CMDeviceMotionH

时间:2018-05-22 15:10:59

标签: ios swift

我最近升级了我的xcode。我目前正在使用9.2 我正在使用CMMotionManger,此错误显示在新版本中。我尝试解决它但找不到解决方案。

func startCameraTracking() {
        motionManager.deviceMotionUpdateInterval = 1.0 / 60.0


        motionManager.startDeviceMotionUpdatesToQueue(OperationQueue.main) {
            [weak self](data: CMDeviceMotion?, error: NSError?) in

            guard let data = data else { return }

            let attitude: CMAttitude = data.attitude
            self?.cameraNode.eulerAngles = SCNVector3Make(Float(attitude.roll + M_PI/2.0), -Float(attitude.yaw), -Float(attitude.pitch))
        }
    }

1 个答案:

答案 0 :(得分:2)

根据迈克尔的建议,您最好查看最新的参考资料:

startDeviceMotionUpdatesToQueue(_: withHandler:)重命名为startDeviceMotionUpdates(to:withHandler:)。)

startDeviceMotionUpdates(to:withHandler:)

  

声明

func startDeviceMotionUpdates(to queue: OperationQueue, 
                  withHandler handler: @escaping CMDeviceMotionHandler)

CMDeviceMotionHandler

  

声明

typealias CMDeviceMotionHandler = (CMDeviceMotion?, Error?) -> Void

对于startDeviceMotionUpdates(to:withHandler:)的第二个参数,您需要传递一个关注CMDeviceMotion?Error?,而不是NSError?

func startCameraTracking() {
    motionManager.deviceMotionUpdateInterval = 1.0 / 60.0

    motionManager.startDeviceMotionUpdates(to: OperationQueue.main) {
        [weak self](data: CMDeviceMotion?, error: Error?) in

        guard let data = data else { return }

        let attitude: CMAttitude = data.attitude
        self?.cameraNode.eulerAngles = SCNVector3Make(Float(attitude.roll + .pi/2.0), -Float(attitude.yaw), -Float(attitude.pitch))
    }
}