Jojo44 Jojo44 - 6 months ago 42
Swift Question

Swift - Incorrect argument label in call with bindToPort

I'm trying to use CocoaAsyncSocket library with Swift.

I would like to implement a UDP server and client. I've imported the library and here is one of my methods's implementation:

func setupConnection(){
var error : NSError?
socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())

do {
try socket.bindToPort(PORT, error: &error)
try socket?.connectToHost(IP, onPort: PORT)
try socket.beginReceiving()
} catch _ {
print(error)
}
send("ping")
}


Unfortunatly I got this error on bindToPort:


Incorrect argument label in call (have ':error:', expected
'
:interface:')


Looking at the declaration of the bindToPort method in the library, I have a prototype corresponding to my implementation.

- (BOOL)bindToPort:(UInt16)port error:(NSError **)errPtr


Why do I still got this error even if the prototype is respected?

Answer

Objective-C functions are dynamically adapted to use Swift's error handling paradigm, which throws rather than using taking an NSError param.

If the last non-block parameter of an Objective-C method is of type NSError **, Swift replaces it with the throws keyword, to indicate that the method can throw an error. If the Objective-C method’s error parameter is also its first parameter, Swift attempts to simplify the method name further, by removing the “WithError” or “AndReturnError” suffix, if present, from the first part of the selector. If another method is declared with the resulting selector, the method name is not changed. - Using Swift with Cocoa and Objective-C (Swift 2.2) - Error Handling