2015-06-14 33 views
6

時注意:這也適用於雨燕3.0錯誤使用減少()在雨燕2.0

當我嘗試使用reduce功能,我得到一個錯誤說:

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

我已經想出瞭如何用enumerate()函數做到這一點,但我似乎無法解決這個問題。這裏的代碼返回錯誤的行:

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

回答

15

你這個修復您enumerate()能解決問題的方式相同。在Swift 2中,reduce作爲一個全局函數被刪除,並且已經作爲一個實例方法被添加到符合協議擴展符合SequenceType協議的所有對象上。用法如下。

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

非常感謝!由於reduce()中的兩個值,我不確定。 –