0

当我编写 TCP...

socket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)

那挺好的

但是当我编码UDP时......

udpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)

错误说Type of expression is ambiguous with more context

我不知道我该如何解决。我搜索了“DispatchQueue”和 GCD 的定义,但仍然没有答案。

如果有人可以帮助我,我将不胜感激!!!

override func viewDidLoad(){
    super.viewDidLoad()
    
    udpSocket = GCDAsyncUdpSocket( delegate: self, delegateQueue: DispatchQueue.main)
}


func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?){
    print("didReceiveData")
    
    var host: NSString?
    var port: UInt16 = 0
    
    GCDAsyncUdpSocket.getHost(&host, port: &port, fromAddress: address)
    
    showMessage("From IP: \(String(data: data, encoding: String.Encoding.utf8)!)")
    
    print("From \(host!)")
    
    print("incoming message: \(String(data: data, encoding: String.Encoding.utf8)!)")
    
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?){
    print("didNotConnect \(error!)")
}

func udpSocketDidClose(_ sock: GCDAsyncUdpSocket, withError error: Error?){
    print("斷開連線error: \(error!)")
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?){
    print("didNotSendDataWithTag")
}

func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int){
    print("didSendDataWithTag")
}
4

1 回答 1

0

Since you're using self as the delegate, make sure your class implements the protocol for the delegate.

于 2020-11-23T00:20:39.080 回答