2016-06-01 7 views
2

SwiftでCocoaAsyncSocketライブラリを使用しようとしています。Swift - bindToPortとの呼び出しで引数のラベルが正しくない

UDPサーバーとクライアントを実装したいと思います。私は、ライブラリをインポートしたし、ここに私のメソッドの実装の一つである:私は、図書館でbindToPortメソッドの宣言を見てみると

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

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") 
} 
残念ながら

私はbindToPort上でこのエラーを得ました私の実装に対応するプロトタイプを持っています。

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

なぜプロトタイプを尊重してもこのエラーが発生しますか?

答えて

2

Objective-C関数は、NSErrorパラメータを使用するのではなく、Swiftのエラー処理パラダイム(throws)を使用するように動的に調整されています。

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!私はエラーパラメータを削除し、それは動作します!ありがとう! – Jojo44

+0

これがあなたの質問を解決したら、投票して受け入れてください:) – Alexander

+0

私は答えを受け入れましたが、残念ながら私はそれに投票するには十分な評判がありません。 – Jojo44

関連する問題