简体   繁体   中英

"Type of expression is ambiguous" error about UDP socket with GCD in Swift (DispatchQueue)

When I coding TCP...

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

That's good

But when I coding UDP...

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

the error says Type of expression is ambiguous with more context

I don't know how can I fix it. I searched the definition of "DispatchQueue" and GCD but I still have no answer on it.

If anyone can help me, I would be very grateful !!!

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")
}

由于您使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM