Error when using reduce() in Swift 2.0

1.6k views Asked by At

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 }
}
1

There are 1 answers

1
Mick MacCallum On BEST ANSWER

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 }
}