MetaWear:在MACOSX上的CLI应用程序
问题描述:
我试图按照MetaWear指南启动示例应用程序,位于h ere。我正在迅速遇到的问题是,我正在发生意想不到的崩溃。这里是我的代码是如何组织的MetaWear:在MACOSX上的CLI应用程序
最后,我Podfile包含以下内容:
platform :osx, '10.12.6'
target 'meta-wear' do
use_frameworks!
pod 'MetaWear', '~> 2.9'
end
当我运行应用程序,我得到一个线程异常如下在第一张图片的第5行:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
虽然我当然是一个新的Swift开发人员(noob),但我不知道为什么我无法重现他们的指南。
的Xcode 9.0 塞拉利昂的MacOS版本10.12.6,(这里我想运行此命令行应用程序)添加无限循环
我更新了main.swift课后
更新到ahve如下:
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
答
无限循环,以保持runloop运行的是不是一个很好的习惯。
向您的课程添加完成处理程序,并在完成时停止runloop。
通常的方式来处理在命令行运行循环是这样的:
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)
要你至少需要一个运行循环,严格讲一个CLI一个CLI执行异步任务不是应用程序本身。 – vadian
@vadian我添加了一个无限循环,但仍然得到同样的问题,即使在同一行。 hmmmm – angryip