间接可编码

用于间接 Codable(Encodable, Decodable) 遵循的协议

文档

间接可编码

目标

直接 Codable 遵循的类型别名。必须遵循下文描述的 IndirectlyCodableModel

typealias Target = CALayerModel

codable()

获取 Codable 对象。

codableTypeName

当使类遵循此协议时,无法在继承类中创建 Target 类型的其他别名。因此,您必须使用此属性以通过 NSClassFromString 获取类。

@objc
public class var codableTypeName: String {
    String(reflecting: Target.self)
}

IndirectlyCodableModel

目标

要表示的类型的别名

typealias Target = CALayer

applyProperties to Target

将属性的值应用于目标对象。使用名为 p-x9/KeyPathValue 的库,您可以创建如下所示的 propertyMap。

let propertyMap: [PartialKeyPath<CALayerModel>: ReferenceWritableKeyPathValueApplier<CALayer>] = [
        \.bounds: .init(\.bounds),
         \.position: .init(\.position),
         \.frame: .init(\.frame),
         \.backgroundColor: .init(\.backgroundColor),
         \.cornerRadius: .init(\.cornerRadius),
         \.borderWidth: .init(\.borderWidth),
         \.borderColor: .init(\.borderColor)
    ]

func applyProperties(to target: CALayer) {
    Self.propertyMap.forEach { keyPath, applier in
        var value = self[keyPath: keyPath]
        if let convertible = value as? (any IndirectlyCodableModel),
            let converted = convertible.converted() {
            value = converted
        }
        applier.apply(value, target)
    }
}

reverseApplyProperties with Target

将值从目标对象应用于其自身属性。

许可

MIT 许可证