Error using reduce () in Swift 2.0

Note. This also applies to Swift 3.0.

When I try to use the reduce function, I get an error message:

reduce is not available: call the reduce () method in the sequence

I already figured out how to do this with the enumerate() function, but I cannot solve this problem. Here is the line of code that returns the error:

 var hashValue: Int { return reduce(blocks, 0) { $0.hashValue ^ $1.hashValue } } 
+6
source share
1 answer

You fix it the same way you fixed your problem with enumerate() . In Swift 2, the abbreviation was removed as a global function and was added as an instance method for all objects that conform to the SequenceType protocol through a protocol extension. Usage is as follows.

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

Source: https://habr.com/ru/post/989076/


All Articles