美文网首页
Swift中使用WCDB保存自定义枚举

Swift中使用WCDB保存自定义枚举

作者: 李有钱灬 | 来源:发表于2024-09-05 11:39 被阅读0次

WCDB中要保存自定义枚举,需要实现ColumnCodable协议,ColumnCodable协议包含ColumnDecodableColumnEncodable
示例:

/// 定义枚举
enum MyEnum: Int32, ColumnCodable {
    case oneEnum = 1
    case twoEnum = 2
    case threeEnum = 3
    case fourEnum = 4

  static var columnType: ColumnType {
    .integer32
  }
  /// ColumnDecodable
  init?(with value: Value) {
    self.init(rawValue: value.int32Value)
  }
  /// ColumnEncodable
  func archivedValue() -> Value {
    Value(self.rawValue)
  }
 }

注意:在decode和encode方法中不要将Value改成基础数据类型(有文章说需要将Value改成Int32),否则会一直报错

之后按一般的基础数据保存即可。

相关文章

网友评论

      本文标题:Swift中使用WCDB保存自定义枚举

      本文链接:https://www.haomeiwen.com/subject/wowrljtx.html