Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using reduce() in Swift 2.0

Note: This also applies for Swift 3.0

When I attempt to use the reduce function, I get an error saying:

reduce is unavailable: call the 'reduce()' method on the sequence

I already figured out how to do this with the enumerate() function but I cannot seem to solve this issue. Here is the line of code returning the error:

var hashValue: Int {
    return reduce(blocks, 0) { $0.hashValue ^ $1.hashValue }
}
like image 408
Gary Simcox Avatar asked Jun 14 '15 03:06

Gary Simcox


1 Answers

You fix this the same way that you fixed your problem with enumerate(). In Swift 2, reduce has been removed as a global function and has been added as an instance method on all objects that conform to the SequenceType protocol via a protocol extension. Usage is as follows.

var hashValue: Int {
    return blocks.reduce(0) { $0.hashValue ^ $1.hashValue }
}
like image 193
Mick MacCallum Avatar answered Oct 19 '22 06:10

Mick MacCallum