Little Xcode Beta Surprises 🎁: Core Graphics Codable Conformance
⭐️⭐️⭐️⭐️⭐
Hi! This post has moved to a new blog! Come to fivestars.blog for the latest articles!
⭐️⭐️⭐️⭐️⭐
If you have dove straight into Swift 4’s Codable protocol as early as Xcode 9 Beta 1, while firing up one of the latest betas (3 or later) you might have seen a warning similar to this:

The Core Graphics Framework is not open source as is Swift, so it wasn’t something that we could have seen coming. Still, this is great news!
Geometric Data Types Codable Conformance
It turns out that every single CoreGraphics Geometric Data Type now conforms to Codable, namely:
CGFloatCGPointCGSizeCGRectCGVectorCGAffineTransform
Awesome! How do we take advantage of this?
Following the new Conformance
If we store CGPoints/CGSizes/etc somewhere, we must now make sure to do so in the same way as the new Codable conformance expect them to.
Let’s find out how!
Try 1: Source Code
The easiest way would be to look at the source code, if we go to the CGPoint definition for example, this is what we’ll see:

Again, the Core Graphics Framework is not open source, so we get to see only the headers and the definitions of the public functions. This won’t help us understand how things work behind the scene, let’s try another way…
Try 2: Apple’s Documentation
My second best guess was Apple’s Documentation, boy was it disappointing:

Try 3: The Hard Way (Playgrounds!)
Tired of guessing new ways to uncover the mystery, I’ve decided to do things the easy way. I’ve fired up a new playground and start coding:

So there you have it! 🎉🎉🎉
The Undocumented CoreGraphics Conformance
To make a quick recap, this is how you store/read the data (as seen in the Playground above) :
CGFloatExpect just a number, with an optional decimal point.to split the integer part from the fractional part (if there’s a fractional part)CGPointExpect an array of two CGFloats, the first represents thex, the second theyCGSizeExpect an array of two CGFloats, the first represents thewidth, the second theheightCGVectorExpect an array of two CGFloats, the first represents thedx, the second thedyCGRectExpect an array of two arrays of twoCGFloat, the first represents theorigin (CGPoint), the second array thesize (CGSize)CGAffineTransformExpect an array of six CGFloats, representing (in order)a,b,c,d,tx, andty
Conclusions
It’s great that Apple has decided to start conforming its Frameworks Data Objects to Codable, I’m looking forward to more and more Apple’s Framework jump on the Codable wagon! (please do Core Location next?)
Update: Source Code
(2017–08–01) While my mission is now complete, Bartosz Polaczyk has pointed out that there’s an actual way to see the
Codableconformance source code 🎉
It turns out that, if we look at the Swift source code, we have the complete CoreGraphics Codable conformance here!
Everything written above is still 100% true and perfectly valid, but it’s awesome to have the actual code: thank you Bartosz Polaczyk 🙌
Federico is a Bangkok-based Software Engineer with a strong passion for Swift, Minimalism, Design, and iOS Development.




