用于间接 Codable
(Encodable
, Decodable
) 遵循的协议
直接 Codable
遵循的类型别名。必须遵循下文描述的 IndirectlyCodableModel
。
typealias Target = CALayerModel
获取 Codable
对象。
当使类遵循此协议时,无法在继承类中创建 Target
类型的其他别名。因此,您必须使用此属性以通过 NSClassFromString
获取类。
@objc
public class var codableTypeName: String {
String(reflecting: Target.self)
}
要表示的类型的别名
typealias Target = CALayer
将属性的值应用于目标对象。使用名为 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)
}
}
将值从目标对象应用于其自身属性。