所以我是swift和iOS编程的新手。我正在尝试记录加速度计和陀螺仪日期并在屏幕上显示。通过一些教程,我设法完成了我的代码并且理解了代码。 但我不断收到错误。如果我遵循xcode给我的建议我只会得到更多错误..
所以这是我的代码:
import UIKit
import CoreMotion
class ViewController: UIViewController {
//instance variables
var currentMaxAccelX: Double = 0.0
var currentMaxAccelY: Double = 0.0
var currentMaxAccelZ: Double = 0.0
var currentMaxRotX: Double = 0.0
var currentMaxRotY: Double = 0.0
var currentMaxRotZ: Double = 0.0
var motionManager = CMMotionManager()
//outlets
@IBOutlet var accX: UILabel?
@IBOutlet var accY: UILabel?
@IBOutlet var accZ: UILabel?
@IBOutlet var maxAccX: UILabel?
@IBOutlet var maxAccY: UILabel?
@IBOutlet var maxAccZ: 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?
//functions
@IBAction func resetMaxValues() {
currentMaxAccelX = 0
currentMaxAccelY = 0
currentMaxAccelZ = 0
currentMaxRotX = 0
currentMaxRotY = 0
currentMaxRotZ = 0
}
override func viewDidLoad() {
self.resetMaxValues()
motionManager.accelerometerUpdateInterval = 0.2
motionManager.gyroUpdateInterval = 0.2
//start recording data
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: {(accelerometerData: CMAccelerometerData!, error: NSError!) -> Void in
self.outputAccelerometerData(accelerometerData.acceleration)
if (error != nil) {
print("\(error)")
}
})
motionManager.startGyroUpdatesToQueue((NSOperationQueue.currentQueue()), withHandler: {(gyroData: CMGyroData!, error: NSError!) -> Void in
self.outputRotationData(gyroData.rotationRate)
if (error! = nil) {
print("\(error)")
}
})
super.viewDidLoad()
}
func outputAccelerometerData(acceleration: CMAcceleration) {
accX?.text = "\(acceleration.x).2fg"
if fabs(acceleration.x) > fabs(currentMaxAccelX) {
currentMaxAccelX = acceleration.x
}
accY?.text = "\(acceleration.y).2fg"
if fabs(acceleration.y) > fabs(currentMaxAccelY) {
currentMaxAccelY = acceleration.y
}
accZ?.text = "\(acceleration.z).2fg"
if fabs(acceleration.z) > fabs(currentMaxAccelZ) {
currentMaxAccelZ = acceleration.z
}
maxAccX?.text = "\(currentMaxAccelX) .2f"
maxAccY?.text = "\(currentMaxAccelY) .2f"
maxAccZ?.text = "\(currentMaxAccelZ) .2f"
}
func outputRotationData(rotation: CMRotationRate) {
rotX?.text = "\(rotation.x).2fr/s"
if fabs(rotation.x) > fabs(currentMaxRotX) {
currentMaxRotX = rotation.x
}
rotY?.text = "\(rotation.y).2fr/s"
if fabs(rotation.y) > fabs(currentMaxRotY) {
currentMaxRotY = rotation.y
}
rotZ?.text = "\(rotation.z).2fr/s"
if fabs(rotation.z) > fabs(currentMaxRotZ) {
currentMaxRotZ = rotation.z
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我一直想要开始记录数据的错误;可选类型'NSOperationQueue?'的值没有打开;你的意思是用“!”或“?”?
对不起,如果这是一个愚蠢的问题,但我真的想了解这些东西!
提前致谢!!
答案 0 :(得分:0)
由于24 %let _path = C:\;
25 %let _name = AOL.xlsx;
26 %let sheet_list = AG ST;
27
28 %macro Import(dlm=%str( ));
29 %local current_sheet i;
30 %let i = 1;
31 %let current_sheet = %scan(%superq(sheet_list),&i,&dlm);
32 %do %while(%superq(current_sheet) ne);
33 %put proc import out=SQ_¤t_sheet. replace datafile="&_path.\&_name.";
34 %put sheet="¤t_sheet.";
35 %put run;
36 %let i = %eval(&i + 1);
37 %let current_sheet = %scan(%superq(sheet_list),&i,&dlm);
38 %end;
39 %mend Import;
40
41 %Import;
proc import out=SQ_AG replace datafile="C:\\AOL.xlsx"
sheet="AG"
run
proc import out=SQ_ST replace datafile="C:\\AOL.xlsx"
sheet="ST"
run
42
43 %let sheet_list = AG 11, AG 12, ST;
44 %import(dlm=%str(,));
proc import out=SQ_AG 11 replace datafile="C:\\AOL.xlsx"
sheet="AG 11"
run
proc import out=SQ_AG 12 replace datafile="C:\\AOL.xlsx"
sheet="AG 12"
run
proc import out=SQ_ST replace datafile="C:\\AOL.xlsx"
sheet="ST"
run
是可选的,您需要使用NSOperationQueue
强制解包它。
因此,请将!
替换为NSOperationQueue.currentQueue()
。
您可以通过此Apple Documentation和此blog来了解Swift选项。
答案 1 :(得分:0)
问题是
NSOperationQueue.currentQueue()
返回一个可选值(参考NSOperationQueue Class Reference)。但是,
startAccelerometerUpdatesToQueue:withHandler:
startGyroUpdatesToQueue: withHandler:
需要一个展开的NSOperationQueue
对象(请参阅:CMMotionManager Class Reference),但在您的代码中,您传递的是可选值,这就是您收到这些错误的原因。
要进行修复,请将这些方法调用更改为:
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: {(accelerometerData: CMAccelerometerData!, error: NSError!) -> Void in
{
})
motionManager.startGyroUpdatesToQueue((NSOperationQueue.currentQueue()!), withHandler: {(gyroData: CMGyroData!, error: NSError!) -> Void in
{
})