8

Xcode 6 プレイグラウンドに次のコードがあります。

import Cocoa
import IOBluetooth

class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
    func deviceInquiryComplete(sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
        aborted
        var devices = sender.foundDevices()
        for device : AnyObject in devices {
            if let thingy = device as? IOBluetoothDevice {
                thingy.getAddress()
            }
        }
    }
}

var inquiry = IOBluetoothDeviceInquiry(delegate: BlueDelegate())
inquiry.start()

OSX で Bluetooth を使い始めたばかりで、現在必要なのは範囲内のデバイスのリストだけです。

私のデリゲートメソッドをまったく呼び出していないようです。

私は OSX 開発と Swift の初心者なので、優しくしてください。:)

4

1 に答える 1

7

コードがバックグラウンドで何かを実行していることを Playground に伝えるには、import XCPlaygroundを呼び出して呼び出す必要がありますXCPSetExecutionShouldContinueIndefinitely()
これにより、プレイグラウンドで IOBluetoothDeviceInquiry が有効なままになり、終了時にデリゲート メソッドを呼び出すことができます。

import Cocoa
import IOBluetooth
import XCPlayground

class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
    func deviceInquiryComplete(sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
        aborted
        println("called")
        var devices = sender.foundDevices()
        for device : AnyObject in devices {
            if let thingy = device as? IOBluetoothDevice {
                thingy.getAddress()
            }
        }
    }
}

var delegate = BlueDelegate()
var inquiry = IOBluetoothDeviceInquiry(delegate: delegate)
inquiry.start()
XCPSetExecutionShouldContinueIndefinitely()

上記のアプローチは機能しますが、非同期コード、委任などの概念を必要とするタスク用のシンプルで従来のテスト プロジェクトを作成する方が簡単だと思います。

于 2014-06-20T07:59:36.587 に答える