Little Xcode Beta Surprises 🎁: Core Graphics Codable Conformance

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:

  • CGFloat
  • CGPoint
  • CGSize
  • CGRect
  • CGVector
  • CGAffineTransform

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:

Closed Source won’t show us any useful information

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:

No overview available 🤣

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:

Can’t go wrong with Playgrounds! (code here)

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) :

  • CGFloat
    Expect just a number, with an optional decimal point . to split the integer part from the fractional part (if there’s a fractional part)
  • CGPoint
    Expect an array of two CGFloats, the first represents the x, the second the y
  • CGSize
    Expect an array of two CGFloats, the first represents the width, the second the height
  • CGVector
    Expect an array of two CGFloats, the first represents the dx, the second the dy
  • CGRect
    Expect an array of two arrays of two CGFloat, the first represents the origin (CGPoint), the second array the size (CGSize)
  • CGAffineTransform
    Expect an array of six CGFloats, representing (in order) a, b, c, d,tx, and ty


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 Codable conformance 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.