首页 > 解决方案 > 如何使用枚举值作为 GRDB 记录中的列?

问题描述

我想记录4种不同的类型。我有以下代码

import GRDB

enum RecordType: Int16 {
    case video, image, text, rep
}
extension RecordType: DatabaseValueConvertible {}

struct Record: Codable, FetchableRecord, PersistableRecord {
    var id: Int64?
    var type: RecordType
}

现在它抱怨Type 'Record' does not conform to protocol 'Decodable'

当然,当我从结构中删除类型时,这种抱怨就消失了。由于类型在技术上是 Int16,为什么这使它不可解码?

标签: iosswiftgrdb

解决方案


当我对 RecordType 采用 Codable 时,这种情况就消失了。在这里找到了答案

extension RecordType: DatabaseValueConvertible, Codable {}

推荐阅读