二元运算符“> =”不能被应用于类型的操作数“(UnsafePointer ,Int32)将 - > UnsafeMutablePointer ”和“INT”
我试图使用用于循环遍历一个优先级队列二元运算符“> =”不能被应用于类型的操作数“(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
你的属性名为intex
(注意错字),因此该函数中的index
指的是发生在是一个名为index
的函数。
非常感谢,这是我的错误,xcode应该给出相关的错误信息 –
@OmarFaruqe:好的,Xcode实际上在这里给出了一个正确的错误信息。在你的(错误的)代码中,'index'指的是全局函数'public func index(_:UnsafePointer
你应该避开说'index ++'的习惯,因为这个语言特性将在未来版本的Swift中被删除。 – matt