Swiftpack.co - p-x9/KeyPathValue as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by p-x9.
p-x9/KeyPathValue 0.0.1
🔑 Structure for assigning values using keypath
⭐️ 5
🕓 29 weeks ago
.package(url: "https://github.com/p-x9/KeyPathValue.git", from: "0.0.1")

KeyPathValue

Structure for assigning values using keypath

Example

ReferenceWritableKeyPathValueApplier

Suppose we have the following model.

struct CALayerModel {
    var bounds: CGRect?
    var position: CGPoint?
    var frame: CGRect?

    var backgroundColor: CGColor?

    var cornerRadius: CGFloat?

    var borderWidth: CGFloat?
    var borderColor: CGColor?
}

Define propertyMap using ReferenceWritableKeyPathValueApplier

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)
    ]

Can be assigned to an object using propertyMap.

extension CALayerModel {
    public func applyProperties(to target: CALayer) {
        Self.propertyMap.forEach { keyPath, applier in
            let value = self[keyPath: keyPath]
            applier.apply(value, target)
        }
    }
}

GitHub

link
Stars: 5
Last commit: 20 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v0.0.1
29 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics