gpt4 book ai didi

swift 陀螺仪 : Unexpectedly found nil while unwrapping an Optional value

转载 作者:行者123 更新时间:2023-11-30 14:05:07 29 4
gpt4 key购买 nike

我正在使用 Core Motion,并且收到“展开可选值时意外发现 nil”,但只有大约一半的时间加载应用程序。如何更正下面的代码,使其稳定?

import UIKit
import CoreMotion

class ViewController: UIViewController {

var currentMaxRotX: Double = 0.0
var currentMaxRotY: Double = 0.0
var currentMaxRotZ: Double = 0.0

let motionManager = CMMotionManager()

@IBOutlet var RollLabel: UILabel!
@IBOutlet var PitchLabel: UILabel!
@IBOutlet var YawLabel: UILabel!

@IBOutlet var rotX: UILabel!
@IBOutlet var rotY: UILabel!
@IBOutlet var rotZ: UILabel!

@IBOutlet var maxRotX: UILabel!
@IBOutlet var maxRotY: UILabel!
@IBOutlet var maxRotZ: UILabel!


@IBOutlet var readOutLabel: UILabel!

override func viewDidLoad() {

if motionManager.gyroAvailable {

motionManager.startGyroUpdates()
motionManager.deviceMotionUpdateInterval = 0.2
motionManager.startDeviceMotionUpdates()

motionManager.gyroUpdateInterval = 0.2
motionManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()) {
[weak self] (gyroData: CMGyroData!, error: NSError!) in

if self?.outputRotationData(gyroData.rotationRate) != nil {

self?.outputRotationData(gyroData.rotationRate)
if error != nil {
println("\(error)")
}

}
}

} else {
println("gyro not avail")
}

super.viewDidLoad()
}
// radians to degrees
func radians(fromDegrees degrees: Double) -> Double {
return 180 * degrees / M_PI
}

func outputRotationData(rotation:CMRotationRate)
{
rotX.text = NSString(format:"Rotation X: %.4f",rotation.x) as String
if fabs(rotation.x) > fabs(currentMaxRotX)
{
currentMaxRotX = rotation.x
}

rotY.text = NSString(format:"Rotation Y: %.4f", rotation.y) as String
if fabs(rotation.y) > fabs(currentMaxRotY)
{
currentMaxRotY = rotation.y
}
rotZ.text = NSString(format:"Rotation Z:%.4f", rotation.z) as String
if fabs(rotation.z) > fabs(currentMaxRotZ)
{
currentMaxRotZ = rotation.z
}

maxRotX.text = NSString(format:"Max rotation X: %.4f", currentMaxRotX) as String
maxRotY.text = NSString(format:"Max rotation Y:%.4f", currentMaxRotY) as String
maxRotZ.text = NSString(format:"Max rotation Z:%.4f", currentMaxRotZ) as String

var attitude = CMAttitude()
var motion = CMDeviceMotion()
motion = motionManager.deviceMotion
attitude = motion.attitude


YawLabel.text = NSString (format: "Yaw: %.2f", attitude.yaw) as String
PitchLabel.text = NSString (format: "Pitch: %.2f", attitude.pitch) as String
RollLabel.text = NSString (format: "Roll: %.2f", attitude.roll) as String

if NSString(string: RollLabel.text!).doubleValue <= -1.6 && NSString(string: RollLabel.text!).doubleValue >= -1.2 {
readOutLabel.text = ""
}
else{
readOutLabel.text = ""
}

}
}

最佳答案

我明白了:

var attitude = CMAttitude()
var motion = CMDeviceMotion()

if motionManager.deviceMotion != nil{
motion = motionManager.deviceMotion
attitude = motion.attitude
YawLabel.text = NSString (format: "Yaw: %.2f", attitude.yaw) as String
PitchLabel.text = NSString (format: "Pitch: %.2f", attitude.pitch) as String
RollLabel.text = NSString (format: "Roll: %.2f", attitude.roll) as String
}

关于 swift 陀螺仪 : Unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32529905/

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