Gar*_*cox 6 reduce function swift swift2
注意:这也适用于Swift 3.0
当我尝试使用该reduce功能时,我收到一条错误消息:
reduce不可用:在序列上调用'reduce()'方法
我已经知道如何使用该enumerate()功能执行此操作,但我似乎无法解决此问题.这是返回错误的代码行:
var hashValue: Int {
return reduce(blocks, 0) { $0.hashValue ^ $1.hashValue }
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*lum 15
您可以像解决问题一样修复此问题enumerate().在Swift 2中,reduce已作为全局函数被删除,并已作为实例方法添加到SequenceType通过协议扩展符合协议的所有对象上.用法如下.
var hashValue: Int {
return blocks.reduce(0) { $0.hashValue ^ $1.hashValue }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1579 次 |
| 最近记录: |