1

我正在尝试使用 Swift 来实现 Microsoft Band SDK。尝试设置我的代码时,我不断收到此错误。

class ViewController: UIViewController, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MSBClientManagerDelegate, UIScrollViewDelegate {

我以前从未见过这种情况,但我也从未尝试将 Objective C 示例转换为 Swift。

任何帮助,将不胜感激!

编辑:这是来自 Objective C 的协议

@protocol MSBClientManagerDelegate<NSObject>

- (void)clientManager:(MSBClientManager *)clientManager clientDidConnect:(MSBClient *)client;
- (void)clientManager:(MSBClientManager *)clientManager clientDidDisconnect:(MSBClient *)client;
- (void)clientManager:(MSBClientManager *)clientManager client:(MSBClient *)client didFailToConnectWithError:(NSError *)error;

@end

编辑 2:使用建议的 Swift Helper 类后

这就是我尝试建立连接的方式。

 var clients:NSArray = bandHelper.attachedClients()!
    var firstClient: MSBClient = clients[0] as MSBClient

if (clients.count == 0){
    println("The band is not detected")
    return
}

我不知道应该如何设置

bandHelper.connectClient(firstClient, {completion: (connected:true -> void in)})
println("Please wait...connecting to band")

然后,当尝试向乐队发送照片时,此功能不起作用

bandHelper.client?.personalizationManager.updateMeTileImage(bandScaledImage, { (completionHandler: NSError!) -> Void in
           NSLog("%@", NSError())})

我被使用助手类抛弃了。任何帮助,将不胜感激!

4

1 回答 1

2

示例项目

我为 Microsoft Band Kit iOS 链接了一个示例 Swift 项目,该项目可以向乐队发送触觉。在这里找到链接:http: //droolfactory.blogspot.com/2015/03/ios-swift-example-of-connecting-with.html


Microsoft Band 桥接头

首先将 Objective-C 类转换为与 Swift 一起使用,创建一个 Bridging Header。对于 MicrosoftBandKit-iOS 框架,我的看起来像这样:

#ifndef ModuleName_Bridging_Header_h
#define ModuleName_Bridging_Header_h

#import <MicrosoftBandKit_iOS/MicrosoftBandKit_iOS.h>

#endif

确保将 ModuleName 替换为您的应用程序模块的名称。在以下位置查找有关桥接头文件的更多信息:https ://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html


乐队助手班

接下来,我将 MSBClientManagerDelegate 包装在一个辅助类 (BandManager) 中,该类使用单例来管理 Band。我在这里有一个要点(https://gist.github.com/mthistle/8f6eb30c68a918fc6240

这个要点的代码是:

import Foundation

let kConnectionChangedNotification = "kConnectionChangedNotification"
let kConnectionFailedNotification  = "kConnectionFailedNotification"

private let _SharedBandManagerInstance = BandManager()

class BandManager : NSObject, MSBClientManagerDelegate {

    private(set) var client: MSBClient?
    private var connectionBlock: ((Bool) -> ())?
    private var discoveredClients = [MSBClient]()

    private var clientManager = MSBClientManager.sharedManager()

    class var sharedInstance: BandManager {
        return _SharedBandManagerInstance
    }

    override init() {
        super.init()
        self.clientManager.delegate = self
    }

    func attachedClients() -> [MSBClient]? {
        if let manager = self.clientManager {
            self.discoveredClients = [MSBClient]()
            for client in manager.attachedClients() {
                self.discoveredClients.append(client as! MSBClient)
            }
        }
        return self.discoveredClients
    }

    func disconnectClient(client: MSBClient) {
        if (!client.isDeviceConnected) {
            return;
        }
        if let manager = self.clientManager {
            manager.cancelClientConnection(client)
            self.client = nil
        }
    }

    func connectClient(client: MSBClient, completion: (connected: Bool) -> Void) {
        if (client.isDeviceConnected && self.client == client) {
            if (self.connectionBlock != nil)
            {
                self.connectionBlock!(true)
            }
            return;
        }

        if let connectedClient = self.client {
            self.disconnectClient(client)
        }

        self.connectionBlock = completion;
        self.clientManager.connectClient(client)
    }

    func clientManager(clientManager: MSBClientManager!, clientDidConnect client: MSBClient!) {
        if (self.connectionBlock != nil) {
            self.client = client
            self.connectionBlock!(true)
            self.connectionBlock = nil
        }

        self.fireClientChangeNotification(client)
    }

    func clientManager(clientManager: MSBClientManager!, clientDidDisconnect client: MSBClient!) {
        self.fireClientChangeNotification(client)
    }

    func clientManager(clientManager: MSBClientManager!, client: MSBClient!, didFailToConnectWithError error: NSError!) {
        if error != nil {
            println(error)
        }
        NSNotificationCenter.defaultCenter().postNotificationName(kConnectionFailedNotification, object: self, userInfo: ["client": client])
    }

    func fireClientChangeNotification(client: MSBClient) {
        NSNotificationCenter.defaultCenter().postNotificationName(kConnectionChangedNotification, object: self, userInfo: ["client": client])
    }

}
于 2015-03-30T06:03:04.780 回答