簡體   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