gpt4 book ai didi

swift - 错误: value of optional type 'NSOperationQueue?' not unwrapped; did you man to use '!' or '?' ?

转载 作者:行者123 更新时间:2023-11-30 13:57:53 25 4
gpt4 key购买 nike

所以,我试图从加速度计传感器获取数据,但即使我尝试过“!”,我仍然收到此错误和 '?'和“如果让”。我想不出尝试其他任何东西,奇怪的是,相同的代码可以在我得到的 YouTube 教程上运行。这是代码:

// Instance variables
var currentMaxAccelY: Double = 0.0
var currentMaxAccelZ: Double = 0.0
lazy var motionManager = CMMotionManager()

// functions
override func viewDidLoad() {
currentMaxAccelY = 0.0
currentMaxAccelZ = 0.0

// Do any additional setup after loading the view.
//video version
if (self.motionManager.accelerometerAvailable) {

self.motionManager.accelerometerUpdateInterval = 0.2
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()){[weak self](accelerometerData: CMAccelerometerData!, error: NSError!) in //here i get the error
self.outputAccelerationData(accelerometerData.acceleration)
if (error != nil){
print("\(error)")

}
}
}

else {
print("accelerometer not available")
}

super.viewDidLoad()

}

func outputAccelerationData(acceleration: CMAcceleration){
jumpCounter?.text = "\(acceleration.z).2fg"
y?.text = "\(acceleration.y).2fg"
if fabs(acceleration.y) > fabs(currentMaxAccelY) {
currentMaxAccelY = acceleration.y
}
if fabs(acceleration.z) > fabs(currentMaxAccelZ) {
currentMaxAccelY = acceleration.z
}
yMax?.text = "\(currentMaxAccelY).2f"
zMax?.text = "\(currentMaxAccelZ).2f"

}

非常感谢任何帮助..

最佳答案

这对我有用

let motionManager = CMMotionManager()
if let queue = NSOperationQueue.currentQueue() {
motionManager.startAccelerometerUpdatesToQueue(queue) { (data, error) -> Void in }
}

关于swift - 错误: value of optional type 'NSOperationQueue?' not unwrapped; did you man to use '!' or '?' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33375904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com