我试图使用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
}

10-08 08:44