早上好,我有这个类的DownloadQueue基于 this tutorial
代码:
import Foundation
import UIKit
import SwiftHTTP
public class DownloadQueue<T> {
var request: HTTPTask;
private var top: QNode<T>!
init(){
self.request = HTTPTask();
self.top = QNode<T>();
}
func enQueue(var key: T) { //check for the instance
if (top == nil) {
top = QNode()
} //establish the top node
if (top.key == nil) {
top.key = key;
return
}
var childToUse: QNode<T> = QNode<T>()
var current: QNode = top //cycle through the list of items to get to the end.
while (current.next != nil) {
current = current.next!
} //append a new item
childToUse.key = key;
current.next = childToUse;
}
func deQueue() -> T? { //determine if the key or instance exists
let topitem: T? = self.top?.key
if (topitem == nil) {
return nil
}
//retrieve and queue the next item
var queueitem: T? = top.key! //use optional binding
if let nextitem = top.next
{
top = nextitem
} else {
top = nil
}
return queueitem
}
func isEmpty() -> Bool {
//determine if the key or instance exist
if let topitem: T = self.top?.key
{
return false
} else {
return true
}
}
func addDownload(url: NSString){
enQueue(url as T);
while (!isEmpty()){
var item: T? = deQueue();
println(item as NSString);
}
}
}
班级QNode
import Foundation
class QNode<T> {
var key: T? = nil
var next: QNode? = nil
}
当我尝试从另一个类中将此类从另一个类中编写此行时,问题就出现了:
var queue: DownloadQueue<T> = DownloadQueue<T>() ;
生成错误
使用未声明的类型T;
我现在不知道为什么而且我发疯了。有人能帮助我吗?
由于
答案 0 :(得分:0)
T
是实例化通用类时必须指定的具体类型的占位符,例如,如果DownloadQueue
必须处理Int
类型,则必须将其实例化为:
var queue = DownloadQueue<Int>()
建议阅读:Generics