我正试图做这种事情..
static var recycle: [Type: [CellThing]] = []
Run Code Online (Sandbox Code Playgroud)
但是 - 我不能:)
未声明的类型'类型'
在这个例子中,CellThing是我的基类,所以A:CellThing,B:CellThing,C:CellThing等等.我的想法是将各种AAA,BB,CCCC存储在字典数组中.
如何制作一个"类型"(理想情况下我猜,限制在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
}
Run Code Online (Sandbox Code Playgroud)
那么就像......
static var recycle: [Type: [CellThing]] = []
private class func makeHelper<T: CellThing>(...)->T {
let c = instantiateViewController(...) as! T
let t = type whatever of c (so, …Run Code Online (Sandbox Code Playgroud) 我有一个简单的枚举,我想迭代.为此,我采用了Sequence和IteratorProtocol,如下面的代码所示.顺便说一句,这可以复制/粘贴到Xcode 8中的Playground.
import UIKit
enum Sections: Int {
case Section0 = 0
case Section1
case Section2
}
extension Sections : Sequence {
func makeIterator() -> SectionsGenerator {
return SectionsGenerator()
}
struct SectionsGenerator: IteratorProtocol {
var currentSection = 0
mutating func next() -> Sections? {
guard let item = Sections(rawValue:currentSection) else {
return nil
}
currentSection += 1
return item
}
}
}
for section in Sections {
print(section)
}
Run Code Online (Sandbox Code Playgroud)
但是for-in循环生成错误消息"Type'Sections.Type'不符合协议'Sequence'".协议一致性在我的扩展中; 那么,这段代码有什么问题?
我知道还有其他方法可以做到这一点,但我想了解这种方法有什么问题.
谢谢.