gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:05:11 24 4
gpt4 key购买 nike

注意:这也适用于 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 作为全局函数被移除,并通过协议(protocol)扩展添加为所有符合 SequenceType 协议(protocol)的对象的实例方法。用法如下。

var hashValue: Int {
return blocks.reduce(0) { $0.hashValue ^ $1.hashValue }
}

关于function - 在 Swift 2.0 中使用 reduce() 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30825671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com