Swift无法键入包含返回布尔的闭包的推断变量
我有一个方法,它包含以下内容:Swift无法键入包含返回布尔的闭包的推断变量,swift,closures,type-inference,Swift,Closures,Type Inference,我有一个方法,它包含以下内容: let downloadNextPagination = { if current.integerValue < amount.integerValue { if current.integerValue != amount.integerValue - 1 { return true } } return false } if downloadNextPagination()
let downloadNextPagination = {
if current.integerValue < amount.integerValue {
if current.integerValue != amount.integerValue - 1 {
return true
}
}
return false
}
if downloadNextPagination() {
// Do something
}
解决了这个问题。问题是:为什么Swift不能在这里计算出正确的关闭类型?所有代码路径都返回Bool,但这无法解决?这是一个基本的限制/我在这里的理解中遗漏了什么,还是这仅仅是一个提高编译器类型推断能力的问题,类似的问题很可能会在Swift 3中得到解决?Swift目前只能推断单返回闭包。您可以将闭包重写为:
let downloadNextPagination = {
return current.integerValue < amount.integerValue && current.integerValue != amount.integerValue - 1
}
let downloadNextPagination={
返回current.integerValue
返回类型仅针对“单表达式闭包”(或从上下文)自动推断。比较一个类似的问题。
let downloadNextPagination = {
return current.integerValue < amount.integerValue && current.integerValue != amount.integerValue - 1
}