function – 在Swift 2.0中使用reduce()时出错

前端之家收集整理的这篇文章主要介绍了function – 在Swift 2.0中使用reduce()时出错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
注意:这也适用于 Swift 3.0

当我尝试使用reduce函数时,我收到一条错误消息:

reduce is unavailable: call the ‘reduce()’ method on the sequence

我已经知道如何使用enumerate()函数执行此操作,但我似乎无法解决此问题.这是返回错误代码行:

var hashValue: Int {
    return reduce(blocks,0) { $0.hashValue ^ $1.hashValue }
}
你修复它的方法与使用enumerate()修复问题的方法相同.在Swift 2中,reduce已作为全局函数删除,并已作为实例方法添加到通过协议扩展符合SequenceType协议的所有对象上.用法如下.
var hashValue: Int {
    return blocks.reduce(0) { $0.hashValue ^ $1.hashValue }
}
原文链接:https://www.f2er.com/swift/318980.html

猜你在找的Swift相关文章