我試圖使用用於循環遍歷一個優先級隊列二元運算符「> =」不能被應用於類型的操作數「(UnsafePointer <Int8>,Int32)將 - > UnsafeMutablePointer <Int8>」和「INT」
測試用例是這樣
var i = 2
for item in queue {
assert(item == i--)
}
我已經延長我原來PriorityQueue
這樣
extension PriorityQueue {
private func getItemsInPriorityOrder() -> [I] {
if !isSorted {
isSorted = true
queue.sortInPlace{
return ($0 == nil) ? true :
($1 == nil) ? false :
$0.priority > $1.priority
}
}
var items: [I] = []
for i in 1..<queue.count {
items.append(queue[i].item)
}
return items
}
}
而且
extension PriorityQueue: SequenceType {
public func generate() -> _PriorityQueueIterator<I> {
return _PriorityQueueIterator<I>(items: getItemsInPriorityOrder())
}
}
public struct _PriorityQueueIterator <I> : GeneratorType {
private let items: [I]
private var intex = 0
init(items: [I]){
self.items = items
}
public mutating func next() -> I? {
return index >= items.count ? nil : items[index++] // this giving the error
}
}
我return語句return index >= items.count ? nil : items[index++]
是給下面的錯誤
我沒有顯式聲明任何東西作爲Int8
類型,Int16
,或者Int32
你應該避開說'index ++'的習慣,因爲這個語言特性將在未來版本的Swift中被刪除。 – matt