ViewController 不符合协议 MSBandClientManagerDelegate
ViewController does not conform to protocol MSBandClientManagerDelegate
我正在尝试使用 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 助手后 class
这就是我尝试建立连接的方式。
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())})
我被助手class 甩掉了。任何帮助将不胜感激!
示例项目
我 link 为 Microsoft Band Kit iOS 编辑了一个示例 Swift 项目,它可以向乐队发送触觉。在此处找到 link:http://droolfactory.blogspot.com/2015/03/ios-swift-example-of-connecting-with.html
Microsoft 带桥接头
首先将 Objective-C classes 转换为与 Swift 一起使用,创建桥接头。对于 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
乐队助手Class
接下来我将 MSBClientManagerDelegate 包装在一个助手 class (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])
}
}
我正在尝试使用 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 助手后 class
这就是我尝试建立连接的方式。
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())})
我被助手class 甩掉了。任何帮助将不胜感激!
示例项目
我 link 为 Microsoft Band Kit iOS 编辑了一个示例 Swift 项目,它可以向乐队发送触觉。在此处找到 link:http://droolfactory.blogspot.com/2015/03/ios-swift-example-of-connecting-with.html
Microsoft 带桥接头
首先将 Objective-C classes 转换为与 Swift 一起使用,创建桥接头。对于 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
乐队助手Class
接下来我将 MSBClientManagerDelegate 包装在一个助手 class (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])
}
}