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.1.0
🔑 Structure for assigning values using keypath
⭐️ 4
🕓 2 weeks ago
.package(url: "https://github.com/p-x9/KeyPathValue.git", from: "0.1.0")

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: 4
Last commit: 2 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v0.1.0
2 weeks ago

What's Changed

New Contributors

Full Changelog: https://github.com/p-x9/KeyPathValue/compare/0.0.1...0.1.0

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