Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用法问题 #3

Open
CoderLouie opened this issue Sep 27, 2021 · 2 comments
Open

用法问题 #3

CoderLouie opened this issue Sep 27, 2021 · 2 comments
Labels
question Further information is requested

Comments

@CoderLouie
Copy link

CoderLouie commented Sep 27, 2021

比如这样一个模型

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"),
       ....
      ]
}

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

@iAllenC
Copy link

iAllenC commented Oct 20, 2021

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

@iwill iwill added the question Further information is requested label Nov 9, 2021
@iwill
Copy link
Owner

iwill commented Nov 9, 2021

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 iwill pinned this issue Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants