Difficulty getting Accelerometer data in iOS
Hi,
I have found some of the tutorial code to read accelerometer data.
@IBAction func startPressed(sender: AnyObject) {
let board = device.board
guard mbl_mw_metawearboard_lookup_module(board, MBL_MW_MODULE_ACCELEROMETER) != MODULE_TYPE_NA else {
print("No accelerometer")
return
}
let signal = mbl_mw_acc_get_acceleration_data_signal(board)
mbl_mw_datasignal_subscribe(signal, bridge(obj: self)) { (context, data) in
let _self: DeviceViewController = bridge(ptr: context!)
let obj: MblMwCartesianFloat = data!.pointee.valueAs()
print(obj)
}
mbl_mw_acc_enable_acceleration_sampling(board)
mbl_mw_acc_start(board)
}
When I use it in my project, using Xcode 10.1 and Swift I get a compile error for the function **mbl_mw_datasignal_subscribe(signal, bridge(obj: self)) **
The error is "A C function pointer cannot be formed from a closure that captures context"
Do you have any later example code that works with the latest xCode and Swift ?
Comments
What Swift version are you using? That Swift code is correct on Swift 4
The code as posted has no errors, but the error you mentioned occurs when you try to access variables from within the closure that are declared externally.
The only way to pass data into the closure is via the context pointer. You would use _self.property if you need to access 'property' from within the mbl_mw_datasignal_subscribe callback
Thank you Stephen, the penny has dropped.
In my code I had added a line which referenced "self" which was the real problem.
Thanks for the help.
I have already implemented both Android and UWP Apps, but am struggling to get to grips with Swift and xCode !!