ExCodable icon indicating copy to clipboard operation
ExCodable copied to clipboard

用法问题

Open CoderLouie opened this issue 3 years ago • 2 comments

比如这样一个模型

struct Coord: ExCodable {
    private(set) var y: Int = 0
    private(set) var x: Int = 0
    private(set) var w: Int = 0
    private(set) var h: Int = 0
    
    static var keyMapping: [KeyMap<Coord>] {
        [KeyMap(\.y, to: "y")]
    }
    init(from decoder: Decoder) throws {
        try decode(from: decoder, with: Self.keyMapping)
    }
}
static var keyMapping: [KeyMap<Coord>] {
      [KeyMap(\.y, to: "y"),
       ....
      ]
}

我需要在这个方法中把所有的属性都列出来吗?可能做到 只列出那些需要做映射的属性,不用做映射的属性可以不列出

CoderLouie avatar Sep 27 '21 09:09 CoderLouie

确实,需要提供全部映射限制性太大了,如果能够找个办法只提供特殊映射,这个库就太好用了

iAllenC avatar Oct 20 '21 05:10 iAllenC

Swift 的限制,实在没有什么好办法,依赖 Swift Runtime 或许可以实现,但是实现方式丑陋无比,可以看下 这里 提到的几个。

另外,develop 分支改用 propertyWrapper 来实现,进一步简化了使用,看起来好了很多,可以试下 —— 还没空改文档、发布以及写文章介绍。

大概这个样子:

struct TestStruct: Equatable, ExAutoCodable {
    @ExCodable // 字段和属性同名可以省掉字段名和括号,但 `@ExCodable` 还是没办法省掉
    var int: Int = 0
    @ExCodable("string", "str", "s", "nested.string") // 支持多个 key 以及嵌套 key 可以这样写
    var string: String? = nil
}

这样集成:

pod 'ExCodable', :git => 'https://github.com/iwill/ExCodable.git', :commit => 'fb605c4'

iwill avatar Nov 09 '21 08:11 iwill