2017-09-26 4 views
0

サンプルアプリケーションの開始時に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 

私はアプリケーションを実行すると、以下のように、私はスレッドの例外を取得します第1の画像の第5行目:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 

私は確かに新しいスウィフトデベロッパー(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は、それ自体はアプリケーションではありません。 – vadian

+0

@vadian無限ループを追加しましたが、同じ行でも同じ問題が発生します。 hmmmm – angryip

答えて

1

無限ループは良くありません習慣。

完了ハンドラをクラスに追加し、完了時に実行ループを停止します。

CLIで実行ループを処理するための通常の方法はこれです:あなたは、少なくとも実行ループを必要と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) 
+0

その説明のためにおかげでvadian。私は他のcliアプリも同様のループを更新するように更新しなければならないでしょう。 &&私はあなたのコードを使用しても、私は同じ正確なエラーを取得します。あなたは何の問題もないと思っていますか? – angryip

+0

私はその特定のポッドを使用していません。 CLIで非同期タスクを実行するという視点から、コードは動作するはずです。 – vadian

+0

に感謝します。将来、フレームワークを使用して問題を解決するなら、私に知らせてください。ご協力いただきありがとうございます。 – angryip

関連する問題