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

标签 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))
        }
    }

最佳答案

根据 Michael 的建议,您最好查看最新的引用资料:

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

startDeviceMotionUpdates(to:withHandler:)

Declaration

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

CMDeviceMotionHandler

Declaration

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

关于ios - 无法将类型 '(CMDeviceMotion?, NSError?) -> ()' 的值转换为预期的参数类型 'CMDeviceMotionH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50471072/

相关文章:

iphone - 未调用 UIImagePickerController 委托(delegate)方法(委托(delegate)设置为自身)

ios - 使用 flowLayout 自动调整 collectionView

ios - 奇怪的Swift崩溃堆栈EXC_BREAKPOINT 0x00000000e7ffdefe

ios - 如何使用属性跟踪混合面板时间事件?

ios - 在 Swift 的 if 语句中使用自定义参数处理错误

ios - 将获取的结果 NSDate 划分为每一天使用核心数据的部分

xcode - UI XCTest, View 中存在的元素

ios - 如何将youtube集成到ios应用程序中?

x代码 7 : how to fix initialization of variable was never used consider replacing with assignment to '_' or removing it

ios - 存储和检索 API 调用的数据