简体   繁体   English

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

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

When I coding TCP...当我编码 TCP 时...

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

That's good那挺好的

But when I coding UDP...但是当我编码 UDP 时...

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

the error says Type of expression is ambiguous with more context错误说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.我搜索了“DispatchQueue”和 GCD 的定义,但仍然没有答案。

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作为委托,请确保您的类为委托实现了协议。

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

相关问题 表达式类型类型错误(Swift 2) - Ambiguous expression type error ( Swift 2 ) 表达式的类型在Swift中不明确 - Type of expression is ambiguous in Swift Swift编译器错误,表达式类型“错误”是模棱两可的,没有更多上下文 - Swift Compiler Error,Expression type 'Error' is ambiguous without more context Swift闭包中的“表达式类型不含更多上下文”错误 - “type of expression is ambiguous without more context” error in Swift closure Swift 2错误:解决“表达式类型在没有更多上下文的情况下是模棱两可的” - Swift 2 Error: Solve “Type of expression is ambiguous without more context” Swift 错误 - 表达式类型不明确,没有更多上下文 - Swift Error - Type of expression is ambiguous without more context Swift 错误:表达式类型不明确,没有更多上下文 - Swift Error: Type of expression is ambiguous without more context Facebook Swift SDK:loginManager表达式类型不明确错误 - Facebook Swift SDK: loginManager Type of expression is ambiguous error 令人困惑的 Swift 错误:表达式类型不明确,没有更多上下文 - Confusing Swift error: Type of expression is ambiguous without more context Swift 2迁移错误:“表达式类型不明确,没有更多上下文” - Swift 2 migration error: “Type of expression is ambiguous without more context ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM