Image for post
Image for post

Swift: CGRect, CGSize & CGPoint

You’re (probably) doing it wrong

Andyy Hope
Feb 2, 2016 · 4 min read

It’s been about a solid eight months since I first decided to jump completely into the Swift wormhole. Over those months I’ve slowly learned to stop writing Objective-C style code patterns with Swift syntax, and started to actually take advantage of the new language.

But one thing I’ve recently picked up on is that I’m still using ugly non-Swift-like syntax with all my CGGeometry structures.

C syntax. A sheep in wolf’s clothing

I have a big feeling that a lot of Swift developers are going to be guilty of this. Raise your hand if you have been using either of these in your code:

Update: As of Swift 3, these functions have been removed.

Yea, I thought so. But don’t worry, it’s nothing to be ashamed of …yet.

The thing that’s wrong with this is that it breaks Swift’s coding convention, it will still work, but it looks a lot more like something that if it belongs in Objective-C or even, dare I say it, Java. *shudders*

To a seasoned iOS or OSX developer, with a short glance they could easily tell you what that code is doing. They don’t need argument labels on CGGeometry structures because everyone knows those arguments, and their sequence off by heart. Except they don’t.

A lot of what Swift is about is being friendly to language newcomers as well as complete programming beginners. If they were to look at that code, they would have no idea what those numbers represent. So let’s all vow to be good Swift citizens and start using the Swift versions:

For a little bit extra syntax, the verbosity of our code is completely understandable at first glance. Also, an added bonus of using the CGGeometry struct initializer is that now we aren’t just confined to using CGFloat as arguments, we can also pass in Int and Double!


Update: As of Swift 3, these functions have been removed.

While we’re at it, you’re probably still using these too, amirite? lel.

We should also be updating these to use the newer, Swiftier syntax. But don’t worry, the syntax is only just an extra character longer. Can you guess what it is?

Another thing to get you on board with this is that Xcode’s syntax highlighter will, depending on your color scheme, highlight the .zero, making it that little bit lighter on your cognitive load.

Value retrieval

Update: As of Swift 3, these functions have been removed.

If you were or still are a good Objective-C citizen, you would have used these value getters when you wanted a specific value of the rect. But wait a second, why don’t we just access the values directly?

For this reason, your applications should avoid directly reading and writing the data stored in the CGRect data structure. Instead, use the functions described here to manipulate rectangles and to retrieve their characteristics.

Apple, CGGeometry Reference Documentation

There’s probably still a lot of you that didn’t bother with such formalities, but that’s ok. Swift has alleviated us of such an unpretty API and instead provided simple dot notation variables.

There’s plenty more examples of these niceties available in the Swift playgrounds I’ve provided to supplement this post. The link will be at the bottom of the page.


Not only are you able to change individual values of a view’s frame, but you’re also able to replace entire sub-structs completely. Behold:

This little feature alone is enough of a reason to stop using Objective-C. Mutability on CGRects without having to create a new one entirely and reset the value. It was only less than two years ago that we as Objective-C developers were forced to write frame modification code like this:

I don’t know about you, but writing that really stressed me out. Creating a newFrame struct from the views frame, modifying it, and then resetting the frame property on the view again. No thank you, not ever, ever again please.

Don’t forget

There are also a couple of other constructs within UIKit for which these changes are applicable to:

Sample code of this post can be found on GitHub.

If you like what you’ve read today you can check our my other articles or want to get in touch, please send me a tweet or follow me on Twitter, it really makes my day. I also organise Playgrounds Conference in Melbourne, Australia and would to see you at the next event.

Swift Programming

The Swift Programming Language

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store