0
通用的初始化我試圖調用一個struct
我從一個協議擴展方法做我也做了:不能調用從協議擴展斯威夫特
public struct AdjacentDifferenceArrayGenerator<T: Strideable where T.Stride == T>: GeneratorType {
private var array: [T]
public init<U: SequenceType where U.Generator.Element == T>(source: U) {
self.array = source.adjacentDifference()
}
public mutating func next() -> [T]? {
guard !array.isEmpty else {
return nil
}
defer {
self.array = self.array.adjacentDifference()
}
return self.array
}
}
public extension SequenceType where Generator.Element: Strideable {
public func adjacentDifference() -> [Self.Generator.Element.Stride] {
var result: [Self.Generator.Element.Stride] = []
var generator = AdjacentDifferenceGenerator(generator: self.generate())
while let difference = generator.next() {
result.append(difference)
}
return result
}
}
public extension SequenceType where Generator.Element: Strideable, Generator.Element.Stride: Strideable, Generator.Element.Stride.Stride == Generator.Element.Stride {
public func pyramidOfAdjacentDifferences() -> [[Self.Generator.Element.Stride]] {
var result: [[Self.Generator.Element.Stride]] = []
var array = self.adjacentDifference()
while !array.isEmpty {
result.append(array)
array = array.adjacentDifference()
}
//var generator = AdjacentDifferenceArrayGenerator(source: self)
//while let differences = generator.next() {
//result.append(differences)
//}
return result
}
正在接近尾聲評論四條線,在替代算法之後,試圖使用與第一個擴展中的方法相同的算法。當我取消註釋掉這些行中的第一行時,遊標在「AdjacentDifferenceArrayGenerator
」處等待,並說:「不能調用類型爲'AdjacentDifferenceArrayGenerator < _>'的初始值設定項,其類型爲'(source:Self)'的參數列表'」最後一個「Self 「在錯誤消息中大寫,儘管它在代碼中沒有大寫。在類型和初始化參數之間添加一個顯式的模板括號沒有幫助。有任何想法嗎?
我有一個「'typealias GType = AdjacentDifferenceArrayGenerator'」並使用該別名來構造'generator',但這並沒有幫助。但是現在我知道元素類型可以從「源」類型中計算出來。 –
CTMacUser