https://github.com/p-x9/keypathvalue
🔑 Structure for assigning values using keypath
https://github.com/p-x9/keypathvalue
swift swiftpackage
Last synced: 8 months ago
JSON representation
🔑 Structure for assigning values using keypath
- Host: GitHub
- URL: https://github.com/p-x9/keypathvalue
- Owner: p-x9
- License: mit
- Created: 2022-11-14T13:06:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-01T14:25:22.000Z (about 2 years ago)
- Last Synced: 2024-10-08T04:31:09.343Z (over 1 year ago)
- Topics: swift, swiftpackage
- Language: Swift
- Homepage:
- Size: 14.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# KeyPathValue
Structure for assigning values using keypath
## Example
### ReferenceWritableKeyPathValueApplier
Suppose we have the following model.
```swift
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`
```swift
let propertyMap: [PartialKeyPath: ReferenceWritableKeyPathValueApplier] = [
\.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.
```swift
extension CALayerModel {
public func applyProperties(to target: CALayer) {
Self.propertyMap.forEach { keyPath, applier in
let value = self[keyPath: keyPath]
applier.apply(value, target)
}
}
}
```