swift - Swift Type 的类型是什么?

标签 swift

我想将类型数组传递给函数 - 但我不确定 Swift 类型的类型是什么。

假设我有两个类: 我的类(class)和另一个类(class)。 我想要一个像这样的数组 [MyClass, AnotherClass]。 数组的类型是什么?

最佳答案

它将是 AnyClass 类型,它是所有对象类型的基础。

func checkClassTypes(types: [AnyClass]) {
  types.forEach { type in
    // Your custom classes don't necessarily have to subclass from NSObject.
    print(type is NSObject.Type)
  }
}

checkClassTypes([MyClass.self, AnotherClass.self])

关于swift - Swift Type 的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39315013/

相关文章:

ios - 执行操作后执行操作?

ios - 如何使 "untargetable"成为 bool 值的选择?

ios - 这行在 Swift 中意味着什么?

ios - "nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection"导致搜索栏出现问题

xcode - 项目从 Objective-C 转换为 Swift 后无法同时满足约束

ios - UITabBar Controller 提供先前选择的索引?

ios - 应用程序不会注册从第一个 ViewController 传递的值

swift - 使用 SKAction 调用带参数的函数

ios - 根据时间 iOS Swift 在 UIBezierPath 上对 UIImageView 进行动画处理

ios - 添加和删​​除某个 UITableViewCell 下面的 UITableViewCell