代码之家  ›  专栏  ›  技术社区  ›  half-potato

MCSessionDelegate要求类符合NSObjectProtocol

  •  1
  • half-potato  · 技术社区  · 10 年前

    我试图使用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 回复  |  直到 10 年前
        1
  •  4
  •   Martin R    10 年前

    这个 MCSessionDelegate 协议源自 NSObjectProtocol . 因此,您的类应该是 NSObject 因此它继承了所有 NSObject协议 方法:

    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
    }