I was trying to use the MultipeerConnectivity library from Apple but my class would not stop throwing the error "Type LocalClient does not conform to NSObjectProtocol". Am I missing anything that would cause this error?
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!) {
}
}
Edit: Formatting.
The MCSessionDelegate
protocol is derived from NSObjectProtocol
. Therefore your class should be a subclass of NSObject
so that it inherits all the NSObjectProtocol
methods:
class LocalClient : NSObject, MCSessionDelegate { }
This also requires you to call super.init()
(after values have been assigned to all properties):
init(name : String) {
peerID = MCPeerID(displayName: name)
session = MCSession(peer: peerID)
availableServers = []
browser = MCBrowserViewController(serviceType: "JoinGame", session: session)
super.init()
session.delegate = self
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.