Chapter 15 Accelerometer Issues

I’m on page 269 and when I tap the screen to play it crashes on line 46 “} as! CMAccelerometerHandler” in CoreMotionHelper.swift. The only error is “Thread 5: EXC_BREAKPOINT (code =1, subcode=0x1000a0ef0)” which isn’t really much help. This also crashes when I use the Chapter 15 sample code provided. Any thoughts?

1 Like

Hi there,

Thanks for bringing this to our attention. There is indeed a small little issue that slipped through the cracks.

To solve it, replace the contents of CoreMotionHelper.swift with the following:

import Foundation
import CoreMotion
class CoreMotionHelper {
let motionManager = CMMotionManager()
init() {}
func getAccelerometerData(interval: TimeInterval = 0.1,
closure: ((_ x: Double, _ y: Double, _ z: Double) → ())? ) {
if motionManager.isAccelerometerAvailable {
motionManager.accelerometerUpdateInterval = interval
motionManager.startAccelerometerUpdates(to: OperationQueue(), withHandler: {
(data: CMAccelerometerData?, error: Error?) → Void in
if closure != nil {
closure!(data!.acceleration.x, data!.acceleration.y, data!.acceleration.z)
}})}}}

I will incorporate this fix into the book ASAP.

Thanks for your support! :]

5 Likes