MetaWear:在MACOSX上的CLI应用程序

问题描述:

我试图按照MetaWear指南启动示例应用程序,位于h ere。我正在迅速遇到的问题是,我正在发生意想不到的崩溃。这里是我的代码是如何组织的MetaWear:在MACOSX上的CLI应用程序

enter image description here

enter image description here

最后,我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 
+0

要你至少需要一个运行循环,严格讲一个CLI一个CLI执行异步任务不是应用程序本身。 – vadian

+0

@vadian我添加了一个无限循环,但仍然得到同样的问题,即使在同一行。 hmmmm – angryip

无限循环,以保持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) 
+0

谢谢你的解释vadian。我将不得不更新我的其他cli应用程序以运行类似的循环。 &&即使我使用你的代码,我也会得到相同的确切错误。我猜你没有得到任何问题? – angryip

+0

我没有使用该特定的吊舱。从在CLI中运行异步任务的角度来看,代码应该可以工作。 – vadian

+0

感谢vadian。如果您将来使用该框架并在我开始之前解决问题,请告诉我。谢谢你的帮助。 – angryip