我正在为我的学校做一个编程主题的项目。我正在 Swift 中使用 Xcode。我想制作一个使用陀螺仪的应用程序。我不知道,但不知怎的,它不会在我的 iPhone 上运行,因为 Xcode 中存在一些我不知道如何修复的错误。当我运行程序时,显示“致命错误:在展开可选值时意外发现 nil”
(lldb)”
我的项目的主要内容是使用陀螺仪在 iPhone 屏幕上以度数显示旋转(偏航、俯仰、滚动)。如果有人帮助我解决这个问题,我将非常感激。先感谢您。
我的 ViewController 中的代码:
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 rotX : UILabel! = nil
@IBOutlet var rotY : UILabel! = nil
@IBOutlet var rotZ : UILabel! = nil
@IBOutlet var maxRotX : UILabel! = nil
@IBOutlet var maxRotY : UILabel! = nil
@IBOutlet var maxRotZ : UILabel! = nil
@IBOutlet weak var RollLabel: UILabel! = nil
@IBOutlet weak var PitchLabel: UILabel! = nil
@IBOutlet weak var YawLabel: UILabel! = nil
override func viewDidLoad() {
if motionManager.gyroAvailable {
if !motionManager.gyroActive {
motionManager.gyroUpdateInterval = 0.2
motionManager.startGyroUpdates()
//motionManager = [[CMMotionManager alloc]init]; should I use this ???
//motionManager.deviceMotionUpdateInterval = 0.2;
//motionManager.startDeviceMotionUpdates()
motionManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: {(gyroData: CMGyroData!, error: NSError!)in
self.outputRotationData(gyroData.rotationRate)
if (error != nil)
{
println("\(error)")
}
})
}
} else {
var alert = UIAlertController(title: "No gyro", message: "Get a Gyro", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
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)
if fabs(rotation.x) > fabs(currentMaxRotX)
{
currentMaxRotX = rotation.x
}
rotY.text = NSString(format:"Rotation Y: %.4f", rotation.y)
if fabs(rotation.y) > fabs(currentMaxRotY)
{
currentMaxRotY = rotation.y
}
rotZ.text = NSString(format:"Rotation Z:%.4f", rotation.z)
if fabs(rotation.z) > fabs(currentMaxRotZ)
{
currentMaxRotZ = rotation.z
}
maxRotX.text = NSString(format:"Max rotation X: %.4f", currentMaxRotX)
maxRotY.text = NSString(format:"Max rotation Y:%.4f", currentMaxRotY)
maxRotZ.text = NSString(format:"Max rotation Z:%.4f", currentMaxRotZ)
var attitude = CMAttitude()
var motion = CMDeviceMotion()
motion = motionManager.deviceMotion
attitude = motion.attitude
YawLabel.text = NSString (format: "Yaw: %.2f", attitude.yaw) //radians to degress NOT WORKING
PitchLabel.text = NSString (format: "Pitch: %.2f", attitude.pitch)//radians to degress NOT WORKING
RollLabel.text = NSString (format: "Roll: %.2f", attitude.roll)//radians to degress NOT WORKING
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在这两行中,您正在使用隐式展开的选项:您无需使用它们即可使用它们?
or !
,但是如果他们nil
。在这种情况下,motionManager.deviceMotion
is nil
因为你还没有打电话motionManager.startDeviceMotionUpdates()
在该访问之前。
Your viewDidLoad
方法是执行此操作的正确位置:
override func viewDidLoad() {
if motionManager.gyroAvailable {
motionManager.deviceMotionUpdateInterval = 0.2;
motionManager.startDeviceMotionUpdates()
motionManager.gyroUpdateInterval = 0.2
motionManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()) {
[weak self] (gyroData: CMGyroData!, error: NSError!) in
self?.outputRotationData(gyroData.rotationRate)
if error != nil {
println("\(error)")
}
}
} else {
// alert message
}
super.viewDidLoad()
}
还有一些额外的变化:
- 你不需要打电话
startGyroUpdates()
before startGyroUpdatesToQueue()
.
- 斯威夫特使用尾随闭包 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-XID_160对于像这样的完成处理程序 - 更好地编写和阅读。
- 为了避免引用循环,声明
self
as weak
在完成处理程序内 - 这需要在访问时使用可选链接self
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)