swift - 检查数组是否可编码
问题描述
我在下面有一个功能:
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)
如何检查数组是否可编码?
不工作:
- case 是 [Codable].Type
- 案例是 Array[Codable].Type
解决方案
我不确定检查的原因,但这应该可行。
if let _ = TaskContainers.self as? Codable.Type {
// Conforms to Codable protocol.
}
推荐阅读
- java - Mapbox:位置符号(冰球)显示在路线下方
- python - 想要从 Flask webapp 中的复制文本中提取电子邮件地址
- javascript - 在轮播中查看时如何激活 JS 文本动画?
- sql - 在 MarkLogic TDE 上 SQL 分组时的奇怪行为
- python - sqlalchemy 文档错了吗?
- mask - 使用图像蒙版像素化/模糊图像部分
- validation - Azure DevOps 中拉取请求验证的用例是什么?
- javascript - 部分代码更好更漂亮的语法
- r - data.table 列表:按变量过滤
- git - 来自另一个分支的结帐文件夹不会删除文件