Generics 未澄清类型“T”的使用
早上好,我有这个基于 守则:Generics 未澄清类型“T”的使用,generics,swift,types,Generics,Swift,Types,早上好,我有这个基于 守则: 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<
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型
我现在不知道为什么,我快疯了。有人能帮我吗
ThankT是具体类型的占位符,在实例化泛型类时必须指定该类型,因此,例如,如果DownloadQueue必须处理Int类型,则必须将其实例化为:
var queue = DownloadQueue<Int>()
建议如下: