繁体   English   中英

关于 Swift 中带有 GCD 的 UDP 套接字的“表达式类型不明确”错误(DispatchQueue)

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

当我编码 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")
}

由于您使用self作为委托,请确保您的类为委托实现了协议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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