我正在尝试按照MetaWear指南启动位于h ere的示例应用程序。我很快遇到的问题是我遇到了意外的崩溃。以下是我的代码的结构
最后,我的Podfile包含以下内容:
platform :osx, '10.12.6'
target 'meta-wear' do
use_frameworks!
pod 'MetaWear', '~> 2.9'
end
当我运行应用程序时,我在第一张图像的第5行得到如下的Thread异常:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
虽然我当然是新的Swift开发人员(noob),但我不知道为什么我无法重现他们的指南。
Xcode:9.0 macOS Sierra版本10.12.6(这是我想运行此命令行应用程序的地方)
添加无限循环后更新
我将main.swift类更新为以下内容:
import Foundation
let runLoop = RunLoop.current;
let distantFuture = Date.distantFuture;
print("### we are in the create");
let starter = MetaWearStarter();
print("### we are after the create");
while (runLoop.run(mode: RunLoopMode.defaultRunLoopMode, before: distantFuture)){
print("### listening for a metawear device");
}
我创建了一个名为MetaWearStarter.swift的类,如下所示:
import Foundation
import MetaWear
class MetaWearStarter : NSObject {
override init() {
super.init();
print("### we are in the init");
startConnection();
}
func startConnection() {
print("##### connection call was made");
let manager = MBLMetaWearManager.shared();
maanger.startScanForMetaWears() { array in
print("### connection scan was complete")
// Hooray! We found a MetaWear board, so stop scanning for more
MBLMetaWearManager.shared().stopScan()
// Connect to the board we found
if let device = array.first {
device.connectAsync().success() { _ in
print("#### we connected to a device");
}.failure() { error in
print("### unable to connect");
}
}
}
}
}
我在这一行收到了上一个错误:
let manager = MBLMetaWearManager.shared();
我的输出从未超越那条线:
### we are in the create
### we are in the init
##### connection call was made
答案 0 :(得分:1)
保持runloop运行的无限循环不是一个好习惯。
为您的班级添加一个完成处理程序,并在完成时停止运行循环。
在CLI中处理运行循环的常用方法是:
import Foundation
import MetaWear
class MetaWearStarter {
let manager = MBLMetaWearManager.shared()
func startConnection(completion: @escaping (String)->()) {
print("##### connection call was made");
manager.startScanForMetaWears() { array in
print("### connection scan was complete")
// Hooray! We found a MetaWear board, so stop scanning for more
manager.stopScan()
// Connect to the board we found
if let device = array.first {
device.connectAsync().success() { _ in
completion("#### we connected to a device")
}.failure() { error in
completion("### unable to connect, error: \(error.localizedDescription)")
}
} else {
completion("#### no device found")
}
}
}
}
let starter = MetaWearStarter()
let runLoop = RunLoop.current
starter.startConnection { (result) in
print(result)
CFRunLoopStop(runLoop.getCFRunLoop())
}
runLoop.run()
exit(EXIT_SUCCESS)