2016-06-01 7 views
2

Ich versuche, die CocoaAsyncSocket-Bibliothek mit Swift zu verwenden.Swift - Falsche Argumentbezeichnung im Aufruf mit bindToPort

Ich möchte einen UDP-Server und -Client implementieren. Ich habe die Bibliothek importiert und hier ist eine meiner Methoden der Umsetzung:

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

Leider habe ich diesen Fehler auf bindToPort:

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

bei der Erklärung des Verfahrens bindToPort Suche in der Bibliothek, ich habe einen Prototyp, der meiner Implementierung entspricht.

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

Warum habe ich diesen Fehler immer noch, selbst wenn der Prototyp respektiert wird?

Antwort

2

Objective-C-Funktionen werden dynamisch angepasst, um das Fehlerbehandlungsparadigma von Swift zu verwenden, das throws anstelle der Verwendung eines 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

+0

Ok! Ich habe den Fehlerparameter entfernt und es funktioniert! Vielen Dank! – Jojo44

+0

Wenn dies Ihre Frage löst, stimmen Sie bitte und akzeptieren Sie :) – Alexander

+0

Ich habe die Antwort akzeptiert, aber leider habe ich nicht genug Ruf, um dafür zu stimmen. – Jojo44