class SQS_Record {
var table: SQS_Table? = nil?
}
class SQS_Table<RecordType: SQS_Record> {
func newRecord() -> RecordType {
let new = RecordType()
new.table = self << ERROR:
return new
}
COMPILER ERROR。不能將'SQS_Table'型別的值分配給'SQS_Table<SQS_Record>'
在未來我希望:class Project。SQS_Record {
var name: String = ""
}
let projectsTbl = SQS_Table<Project>(...__)
并像這樣創建物件
let item = projectsTbl.newRecord()
uj5u.com熱心網友回復:
鑒于你所描述的情況,你正在尋找協議,而不是類。例如:
// TableRecords know their table (and know other things)
//注意TableRecord是一個PAT(帶有關聯型別的協議)。
///不可能有任何 "TableRecord "型別的變數。它只能
//被用來約束其他型別(如下表)。
protocol TableRecord {
init(table: Table<Self> )
var table。Table<Self> { get }
}
//專案是一個具體的TableRecord,它也有一個名字。
//可以有Project型別的變數。。
struct Project。TableRecord {
let table: Table<Self>
var name: String = ""
init(table: Table<Self>) {
self.table = table
}
}
//表可以創建記錄型別的新記錄。
class Table<Record。TableRecord> {
func newRecord() -> Record {
let new = Record(table: self)
return new
}
}
let projects = Table<Project>()
let item = projects.newRecord() // item is of type Project
uj5u.com熱心網友回復:
不可能在兩個有繼承權的類之間的遞回參考中使用通用型別。 為什么我們需要這樣做呢?
- var table: AnyObject? - 欄位可以存盤任何類(ref)
- (table as!
- (table as! SQS_Table).reloadRecord(record: self as! Self) - 基于Self.的異地型別轉換 。
我無法用Protocol解決這個問題。從參考型別(類--不是結構)的繼承和型別轉換--是被禁止的
。class SQS_Record {
var table: AnyObject? = nil?
// Example - access to referenced table method and pass self param
func reload() {
guard __table != nil else { return }
(table as! SQS_Table<Self>).reloadRecord(record: self as! Self)
}
}
class SQS_Table<RecordType: SQS_Record> {
//RecordType - SQS_Record的子類。
func newRecord() -> RecordType {
let new = RecordType()
new.table = self ()
return new
}
func reloadRecord(record: RecordType) {
...。
}
}
class Project: SQS_Record {
...。
}
let projectsTable = SQS_Table<Project>(...__)
let newProjectRecord = projectsTable.newRecord()
newProjectRecord.reload()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318632.html
標籤: