我试图使用Apple的MultipeerConnectivity库,但我的 class 不会停止抛出错误“Type LocalClient不符合NSObjectProtocol”。我是否遗漏了会导致此错误的任何内容?
import Foundation
import MultipeerConnectivity
class LocalClient : MCSessionDelegate {
var availableServers : [Int]
var session : MCSession
var peerID : MCPeerID
var browser : MCBrowserViewController
init(name : String) {
peerID = MCPeerID(displayName: name)
session = MCSession(peer: peerID)
session.delegate = self
browser = MCBrowserViewController(serviceType: "JoinGame", session: session)
}
func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState) {
}
func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!) {
}
func session(session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!) {
}
func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!) {
}
func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!) {
}
}
编辑:格式化。
最佳答案
MCSessionDelegate
协议是从NSObjectProtocol
派生的。
因此,您的类应该是NSObject
的子类,以便它继承所有NSObjectProtocol
方法:
class LocalClient : NSObject, MCSessionDelegate { }
这还要求您调用
super.init()
(在将值分配给所有属性):
init(name : String) {
peerID = MCPeerID(displayName: name)
session = MCSession(peer: peerID)
availableServers = []
browser = MCBrowserViewController(serviceType: "JoinGame", session: session)
super.init()
session.delegate = self
}