【发布时间】:2017-07-16 12:22:56
【问题描述】:
我正在尝试做这种事情..
static var recycle: [Type: [CellThing]] = []
但是 - 我不能:)
未声明的类型“类型”
在示例中,CellThing 是我的基类,所以 A:CellThing、B:CellThing、C:CellThing 等等。我的想法是我会在字典数组中存储各种 A A A、B B、C C C C。
如何使“类型”(理想情况下我猜,仅限于 CellThing)成为 Swift 字典中的键?
我很感激我可以(也许?)使用String(describing: T.self),但这会让我失眠。
这是一个用例,设想的代码看起来像这样......
@discardableResult class func make(...)->Self {
return makeHelper(...)
}
private class func makeHelper<T: CellThing>(...)->T {
let c = instantiateViewController(...) as! T
return c
}
那么像...
static var recycle: [Type: [CellThing]] = []
private class func makeHelper<T: CellThing>(...)->T {
let c = instantiateViewController(...) as! T
let t = type whatever of c (so, maybe "A" or "B")
recycle[t].append( c )
let k = recycle[t].count
print wow, you have k of those already!
return c
}
【问题讨论】:
-
您尝试过 CellThing.self
-
类型可以散列吗?
-
@matt - 我很感激我可能必须使 CellThing 可散列。这真的是问题吗(而且错误只是奇怪的)??
-
是的。它正在寻找一个名为 Type 的类,但没有。我可以使用类作为非静态变量的键: var recycle: [UIView.self: [String]] = [] // 没有编译器错误
-
我也不知道
AnyHashable会有什么帮助;它只能包装Hashable的东西,但元类型不是Hashable。
标签: swift dictionary types