所以我是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?”的值没有包装;你的意思是使用“!”要么 ”?”?

抱歉,如果这是一个愚蠢的问题,但我真的很想了解这些内容!

提前致谢!!

最佳答案

由于NSOperationQueue是可选的,因此您需要使用!强制打开它。

因此,将NSOperationQueue.currentQueue()替换为NSOperationQueue.currentQueue()!

您可以遍历此Apple Documentation和此blog以了解Swift可选内容。

关于ios - Swift可选类型问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33281961/

10-13 06:17