如何将类存储在数组中

nRe*_*wik 1 swift

我想检查一个对象是否属于某种类型。为此,我可以使用is.

if cell is UITableViewCell{
    // do something
}
Run Code Online (Sandbox Code Playgroud)

但是,我正在使用各种类型。所以,我想创建一个数组来存储类的类型。

let types = [ ATableViewCell, BTableViewCell, CTableViewCell]
Run Code Online (Sandbox Code Playgroud)

然后迭代数组并检查

for type in types{
    if cell is type{
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是如何将类型的类型存储到数组中。Swift 2 允许我这样做吗?

谢谢你。

nRe*_*wik 5

我找到了解决方案。这是有关Swift Documentation的解决方案。

let types = [ ATableViewCell.self, BTableViewCell.self, CTableViewCell.self]
for type in types{
    if cell.dynamicType === type{
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)