【问题标题】:Swift 4 from Swift 3 Conversion - Void Expects 2 arguments来自 Swift 3 Conversion 的 Swift 4 - Void 需要 2 个参数
【发布时间】:2019-07-08 15:55:41
【问题描述】:
我在 Swift 4 中遇到错误,但不太确定 Xcode 期望什么。
这段代码:
_connection.on(method: "locationUpdate", callback: {(locationUpdate: LocationUpdate) in
self.locationUpdateReceived(locationUpdate)
})
产生这个错误:
上下文闭包类型 '([Any?], TypeConverter) -> Void' 需要 2 个参数,但在闭包体中使用了 1 个
【问题讨论】:
标签:
swift
type-conversion
【解决方案1】:
你可以试试
_connection.on(method: "locationUpdate", callback: { locationUpdate, other in
self.locationUpdateReceived(locationUpdate)
})
或
_connection.on(method: "locationUpdate") { locationUpdate, other in
self.locationUpdateReceived(locationUpdate)
}
如果您不想使用other 参数,最佳做法是不分配它,如下所示:
_connection.on(method: "locationUpdate") { locationUpdate, _ in
self.locationUpdateReceived(locationUpdate)
}