Swift闭包语法

时间:2017-11-18 17:49:11

标签: swift closures

我有以下代码可以使用:

let obsScan = source.scan(0, accumulator: +)
let obsReduce = source.reduce(0, accumulator: +)

let obs = Observable.zip(obsScan, obsReduce) { scan, reduce in
        return "scan - \(scan), reduce - \(reduce)"
}

我想用自动完全闭包语法重写它,最后我得到了:

let obs = Observable.zip(obsScan, obsReduce, resultSelector: { (scan, reduce) -> _ in
         return "scan - \(scan), reduce - \(reduce)"
})

然而,该代码给我带来了多个错误:

  

闭包参数列表的上下文类型需要2个参数,其中   不能隐式忽略一行上的连续语句必须是   以';'分隔预期表达

我无法理解为什么我使用tab来自动完成闭包函数,当我填充参数时出现错误?

整个函数声明如下:

 public static func zip<O1, O2>(_ source1: O1, _ source2: O2, resultSelector: @escaping (O1.E, O2.E) throws -> RxSwift.Observable.E) -> RxSwift.Observable<RxSwift.Observable.E> where O1 : ObservableType, O2 : ObservableType

1 个答案:

答案 0 :(得分:2)

我不确定你还在等什么。但这应该有效:

let obs = Observable.zip(obsScan, obsReduce, resultSelector: { scan, reduce in
         return "scan - \(scan), reduce - \(reduce)"
})