首页 > 解决方案 > 检查数组是否可编码

问题描述

我在下面有一个功能:

func check(_ type: Any.Type) {
        switch type {
           case is [String].Type
        ...
      }
}

例如:

class TaskContainer: Codable {
    let id: String
    let tasks: [String]
}
typealias TaskContainers = [TaskContainer]
check(TaskContainers.self)

如何检查数组是否可编码?

不工作:

标签: swiftswift4

解决方案


我不确定检查的原因,但这应该可行。

if let _ = TaskContainers.self as? Codable.Type {
    // Conforms to Codable protocol.
}

推荐阅读