2
這裏獲得通用代碼是正常工作的代碼,這是實現迭代器模式的:試圖從工作Iterator模式
這裏是我認爲是由於的通用變化的代碼以上,但我有兩個錯誤代碼(見下面的代碼):
struct Whatevers<T> {
let whatevers: [T]
}
extension Whatevers: Sequence {
func makeIterator() -> Whatevers<T>.Iterator {
return WhateversIterator(sequence: whatevers, current: 0)
}
}
struct WhateversIterator<T>: IteratorProtocol {
let sequence: [T]
var current = 0
mutating func next() -> WhateversIterator.Element? {
defer { current += 1 }
return sequence.count > current ? sequence[current] : nil
}
}
error: MyPlayground.playground:854:1: error: type 'Whatevers' does not conform to protocol 'Sequence' extension Whatevers: Sequence { ^
error: MyPlayground.playground:861:8: error: type 'WhateversIterator' does not conform to protocol 'IteratorProtocol' struct WhateversIterator: IteratorProtocol {
有人能解釋什麼是這個代碼不正確。我怎樣才能使它工作?
非常有幫助。對於泛型迭代器,沒有任何好的例子。謝謝! – GreatBigBore