Skip to content

Commit 79322e9

Browse files
bubskiianpartridge
authored andcommitted
Codable conformance added to some CG types (#1128)
* CGPoint : Codable implemented * CGSize : Codable implemented * CGRect : Codable implemented
1 parent 2b2931f commit 79322e9

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Foundation/NSGeometry.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ extension CGPoint: NSSpecialValueCoding {
7676
}
7777
}
7878

79+
extension CGPoint : Codable {
80+
public init(from decoder: Decoder) throws {
81+
var container = try decoder.unkeyedContainer()
82+
let x = try container.decode(CGFloat.self)
83+
let y = try container.decode(CGFloat.self)
84+
self.init(x: x, y: y)
85+
}
86+
87+
public func encode(to encoder: Encoder) throws {
88+
var container = encoder.unkeyedContainer()
89+
try container.encode(x)
90+
try container.encode(y)
91+
}
92+
}
93+
7994
public struct CGSize {
8095
public var width: CGFloat
8196
public var height: CGFloat
@@ -139,6 +154,21 @@ extension CGSize: NSSpecialValueCoding {
139154
}
140155
}
141156

157+
extension CGSize : Codable {
158+
public init(from decoder: Decoder) throws {
159+
var container = try decoder.unkeyedContainer()
160+
let width = try container.decode(CGFloat.self)
161+
let height = try container.decode(CGFloat.self)
162+
self.init(width: width, height: height)
163+
}
164+
165+
public func encode(to encoder: Encoder) throws {
166+
var container = encoder.unkeyedContainer()
167+
try container.encode(width)
168+
try container.encode(height)
169+
}
170+
}
171+
142172
public struct CGRect {
143173
public var origin: CGPoint
144174
public var size: CGSize
@@ -157,6 +187,21 @@ extension CGRect: Equatable {
157187
}
158188
}
159189

190+
extension CGRect : Codable {
191+
public init(from decoder: Decoder) throws {
192+
var container = try decoder.unkeyedContainer()
193+
let origin = try container.decode(CGPoint.self)
194+
let size = try container.decode(CGSize.self)
195+
self.init(origin: origin, size: size)
196+
}
197+
198+
public func encode(to encoder: Encoder) throws {
199+
var container = encoder.unkeyedContainer()
200+
try container.encode(origin)
201+
try container.encode(size)
202+
}
203+
}
204+
160205
public typealias NSPoint = CGPoint
161206

162207
public typealias NSPointPointer = UnsafeMutablePointer<NSPoint>

TestFoundation/TestCodable.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,54 @@ class TestCodable : XCTestCase {
220220
expectRoundTripEqualityThroughJSON(for: decimal)
221221
}
222222
}
223+
224+
// MARK: - CGPoint
225+
lazy var cgpointValues: [CGPoint] = [
226+
CGPoint(),
227+
CGPoint(x: 10, y: 20),
228+
CGPoint(x: -10, y: -20),
229+
// Disabled due to limit on magnitude in JSON. See SR-5346
230+
// CGPoint(x: .greatestFiniteMagnitude, y: .greatestFiniteMagnitude),
231+
]
232+
233+
func test_CGPoint_JSON() {
234+
for point in cgpointValues {
235+
expectRoundTripEqualityThroughJSON(for: point)
236+
}
237+
}
238+
239+
// MARK: - CGSize
240+
lazy var cgsizeValues: [CGSize] = [
241+
CGSize(),
242+
CGSize(width: 30, height: 40),
243+
CGSize(width: -30, height: -40),
244+
// Disabled due to limit on magnitude in JSON. See SR-5346
245+
// CGSize(width: .greatestFiniteMagnitude, height: .greatestFiniteMagnitude),
246+
]
247+
248+
func test_CGSize_JSON() {
249+
for size in cgsizeValues {
250+
expectRoundTripEqualityThroughJSON(for: size)
251+
}
252+
}
253+
254+
// MARK: - CGRect
255+
lazy var cgrectValues: [CGRect] = [
256+
CGRect(),
257+
CGRect(origin: CGPoint(x: 10, y: 20), size: CGSize(width: 30, height: 40)),
258+
CGRect(origin: CGPoint(x: -10, y: -20), size: CGSize(width: -30, height: -40)),
259+
// Disabled due to limit on magnitude in JSON. See SR-5346
260+
// CGRect(origin: CGPoint(x: -.greatestFiniteMagnitude / 2,
261+
// y: -.greatestFiniteMagnitude / 2),
262+
// size: CGSize(width: .greatestFiniteMagnitude,
263+
// height: .greatestFiniteMagnitude)),
264+
]
265+
266+
func test_CGRect_JSON() {
267+
for rect in cgrectValues {
268+
expectRoundTripEqualityThroughJSON(for: rect)
269+
}
270+
}
223271

224272
}
225273

@@ -235,6 +283,9 @@ extension TestCodable {
235283
("test_IndexPath_JSON", test_IndexPath_JSON),
236284
("test_AffineTransform_JSON", test_AffineTransform_JSON),
237285
("test_Decimal_JSON", test_Decimal_JSON),
286+
("test_CGPoint_JSON", test_CGPoint_JSON),
287+
("test_CGSize_JSON", test_CGSize_JSON),
288+
("test_CGRect_JSON", test_CGRect_JSON),
238289
]
239290
}
240291
}

0 commit comments

Comments
 (0)