MCSessionDelegate要求类符合NSObjectProtocol

时间:2014-08-28 05:28:19

标签: ios swift multipeer-connectivity

我试图使用Apple的MultipeerConnectivity库,但是我的类不会停止抛出错误" 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!) {

    }

}

编辑:格式化。

1 个答案:

答案 0 :(得分:4)

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
}