Swift - 使用 bindToPort 调用时参数标签不正确
Swift - Incorrect argument label in call with bindToPort
我正在尝试将 CocoaAsyncSocket 库与 Swift 一起使用。
我想实现一个 UDP 服务器和客户端。我已经导入了库,这是我的方法之一:
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 上遇到了这个错误:
Incorrect argument label in call (have ':error:', expected
':interface:')
查看库中bindToPort方法的声明,有对应我实现的原型
- (BOOL)bindToPort:(UInt16)port error:(NSError **)errPtr
为什么即使尊重原型,我仍然会出现此错误?
Objective-C 函数动态调整以使用 Swift 的错误处理范例,throws
而不是使用 NSError
参数。
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
我正在尝试将 CocoaAsyncSocket 库与 Swift 一起使用。
我想实现一个 UDP 服务器和客户端。我已经导入了库,这是我的方法之一:
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 上遇到了这个错误:
Incorrect argument label in call (have ':error:', expected ':interface:')
查看库中bindToPort方法的声明,有对应我实现的原型
- (BOOL)bindToPort:(UInt16)port error:(NSError **)errPtr
为什么即使尊重原型,我仍然会出现此错误?
Objective-C 函数动态调整以使用 Swift 的错误处理范例,throws
而不是使用 NSError
参数。
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