Frank Navarrete
Frank Navarrete

Reputation: 121

CMAccelerometerHandler thread1 error

Im trying to experiment with the accelerometer of the iPhone but I'm getting the error "Thread 1:EXC_BREAKPOINT (code=1,subcode=0x1000a3734). I've read that it may be because of a breakpoint, but I deleted all the breakpoints and still the error occurs, also I read it might be because of a nil variable but even if the handler is empty the error still happens. Im at a loss and i'm teaching myself the language. Im using swift 3.0 for this.

import UIKit
import CoreMotion

class ViewController: UIViewController {

//Variables

    var Accelx: Double = 0.0
    var Accely: Double = 0.0
    var Accelz: Double = 0.0
    var Gyrox:  Double = 0.0
    var Gyroy:  Double = 0.0
    var Gyroz:  Double = 0.0

    var motionManager = CMMotionManager()

//IBOutlets
    @IBOutlet var lblX: UILabel?
    @IBOutlet var lblY: UILabel?
    @IBOutlet var lblZ: UILabel?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.resetValues()

        motionManager.accelerometerUpdateInterval = 0.2
        motionManager.startAccelerometerUpdates(to:
            OperationQueue.current!,
            withHandler:
                {(accelData: CMAccelerometerData, Error: NSError)
                    in
                    self.outputAccelData(acceleration:accelData.acceleration)
                    if(Error != nil){
                        //print("\(Error)")
                    }
                } as! CMAccelerometerHandler  )

        super.viewDidLoad()
    }

    func outputAccelData(acceleration: CMAcceleration){
        lblX?.text = "X: \(acceleration.x)"
        lblY?.text = "Y: \(acceleration.y)"
        lblZ?.text = "Y: \(acceleration.z)"
    }
} 

Upvotes: 0

Views: 811

Answers (2)

tailssky
tailssky

Reputation: 1

The handler's second parameter's type should be Error, not NSError.

Upvotes: 0

Frank Navarrete
Frank Navarrete

Reputation: 121

    //Motion Sensor
    if (motionManager.isAccelerometerAvailable){
        motionManager.startAccelerometerUpdates(
            to: OperationQueue.current!,
            withHandler: {(accelData: CMAccelerometerData?, errorOC: Error?) in
                self.outputAccelData(acceleration: accelData!.acceleration)
                    })
    }

    if (motionManager.isGyroAvailable){
        motionManager.startGyroUpdates(
            to: OperationQueue.current!,
            withHandler: { (gyroData: CMGyroData?, errorOC: Error?) in
                self.outputGyroData(gyro: gyroData!)
        })
    }

For anyone who happens to come across this issue, this was the solution. I believe it was that CMAccelerometerData and NSError were not being unwrapped and that the handler CMAccelerometerHandler needs those two to be unwrapped. Also I was trying to cast the anonymous function as a handler.

P.S. Yay! I got a tumbleweed badge!

Upvotes: 2

Related Questions