Little Xcode Beta Surprises 🎁: Core Graphics Codable Conformance

Federico Zanetello
Jul 25, 2017 · 4 min read

⭐️⭐️⭐️⭐️⭐
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:

Surprise!

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

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)

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

Swiftly Swift

Write code swiftly

Federico Zanetello

Written by

Swiftly Swift author, building apps @kimchi_media .

Swiftly Swift

Write code swiftly

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade