HollowCodable 2.1.4

HollowCodable 2.1.4

77 维护。



HollowCodable

CocoaPods Compatible CocoaPods Compatible Platform

HollowCodable 是 Swift 的 property wrappers 库用于可编解码的自定义。

  • 通过声明性注释使复杂的可编解码序列化变得轻松!
struct YourModel: MappingCodable {
    @Immutable
    var id: Int
    
    var url: URL?
    
    @Immutable @BoolCoding
    var bar: Bool?
    
    @SecondsSince1970DateCoding
    var timestamp: Date?
    
    @DateFormatCoding<Hollow.DateFormat.yyyy_mm_dd_hh_mm_ss>
    var time: Date?
    
    @ISO8601DateCoding
    var iso8601: Date?
    
    @HexColorCoding
    var color: HollowColor?
    
    @EnumCoding<TextEnumType>
    var type: TextEnumType?
    
    @DecimalNumberCoding
    var amount: NSDecimalNumber?
    
    @RGBAColorCoding
    var backgroundColor: HollowColor?
    
    var dict: DictAA?
    
    struct DictAA: MappingCodable {
        @DecimalNumberCoding
        var amount: NSDecimalNumber?
    }
    
    static var codingKeys: [ReplaceKeys] {
        return [
            ReplaceKeys.init(replaceKey: "color", originalKey: "hex_color"),
            ReplaceKeys.init(replaceKey: "url", originalKey: "github"),
            ReplaceKeys.init(replaceKey: "backgroundColor", originalKey: "background_color"),
        ]
    }
}
  • 如下所示 json
{
    "code": 200,
    "message": "test case.",
    "data": [{
        "id": 2,
        "title": "Harbeth Framework",
        "imageURL": "https://upload-images.jianshu.io/upload_images/1933747-4bc58b5a94713f99.jpeg",
        "github": "https://github.com/yangKJ/Harbeth",
        "amount": "23.6",
        "hex_color": "#FA6D5B",
        "type": "text1",
        "timestamp" : 590277534,
        "bar": 1,
        "time": "2024-05-29 23:49:55",
        "iso8601": "2023-07-23T23:36:38Z",
        "background_color": {
            "red": 255,
            "green": 128,
            "blue": 128
        },
        "dict": {
            "amount": "52.9",
        }
    }, {
        "id": 7,
        "title": "Network Framework",
        "imageURL": "https://upload-images.jianshu.io/upload_images/1933747-4bc58b5a94713f99.jpeg",
        "github": "https://github.com/yangKJ/RxNetworks",
        "amount": 120.3,
        "hex_color": "#1AC756",
        "type": "text2",
        "timestamp" : 590288534,
        "bar": "yes",
        "time": "2024-05-29 20:23:46",
        "iso8601": "2023-05-23T09:43:38Z",
        "background_color": {
            "red": 200,
            "green": 63,
            "blue": 94
        },
        "dict": {
            "amount": 200,
        }
    }]
}

可用的属性包装器

繁荣

Booming 是 Swift 的一个基础网络库。专为 Swift 5 开发,旨在利用最新的语言功能。框架的最终目标是实现易于使用且易于编写可维护代码的网络。

此模块用于数据序列化和反序列化,取代了 HandyJSON。

🎷 与网络部分一起使用示例

func request(_ count: Int) -> Observable<[CodableModel]> {
    CodableAPI.cache(count)
        .request(callbackQueue: DispatchQueue(label: "request.codable"))
        .deserialized(ApiResponse<[CodableModel]>.self, mapping: CodableModel.self)
        .compactMap({ $0.data })
        .observe(on: MainScheduler.instance)
        .catchAndReturn([])
}

CocoaPods

CocoaPods 是一个依赖关系管理器。有关使用和安装说明,请访问他们的网站。要使用 CocoaPods 集成,请在其 Podfile 中指定

如果您想使用 Codable

pod 'HollowCodable'

备注

一般过程几乎如下,演示也写得非常详细,您可以自行查看。🎷

CodableExample

提示:如果您发现这很有帮助,请为我点一个星。如果您有任何问题或需求,也可以进行提问。

谢谢。🎇

关于作者

请给我买杯咖啡或在 GitHub 支持。

yellow-button

许可证

Booming 可在 MIT 许可证下使用。更多信息请参阅 LICENSE 文件。