-
Notifications
You must be signed in to change notification settings - Fork 14
/
PointCoding.swift
45 lines (33 loc) · 932 Bytes
/
PointCoding.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// PointCoding.swift
// CodableExample
//
// Created by Condy on 2024/6/18.
//
import Foundation
/// `@PointCoding`: For a `CGPoint` property that should be serialized to a x,y encoded dict.
public struct PointValue {
var x: CGFloat?
var y: CGFloat?
init(x: CGFloat, y: CGFloat) {
self.x = x
self.y = y
}
}
extension PointValue: Transformer {
public typealias DecodeType = CGPoint
public typealias EncodeType = PointValue
public init?(value: Any) { }
public func transform() throws -> CGPoint? {
.init(x: x ?? 0, y: y ?? 0)
}
public static func transform(from value: CGPoint) throws -> PointValue {
PointValue.init(x: value.x, y: value.y)
}
}
extension PointValue: DefaultValueProvider {
public typealias DefaultType = CGPoint
public static var hasDefaultValue: DefaultType {
.zero
}
}