Oh dear, Swift 4 sure does change a lot from Swift! That’s the bad news…but there is good news! Swift 4 is much easier to read, and therefore type 🙂

Change 1/4 – All Arguments Have Labels

Normally, Swift 2 didn’t require the first arguments label. Swift 4 does. In addition Swift 4 generally truncates the method name and requires you to add it as the first parameter. A good example is the good old Sections in Table View (Swift 2 on top, Swift 4 on bottom).

Another example:

What about methods you’re calling from frameworks such as UIKit? You need to insert an underscore as the first parameter. Eg:

This seems contradictory to the above changes but hey ho, it’s not totally terrible and I’m sure that Xcode 8 will point you in the right direction with lots of handy red errors 🙂

Change 2/4 – Removing Needless Words

Ever thought that Swift 2 was a little too wordy? Eg:

Gee, I wonder if we can squeeze in another ‘Color’ keyword somewhere (!) Swift 4 is determined to eliminate some of that needless length by removing it. Eg:

Finally, an example of the above 2 changes working together:

I kinda like how Swift 4 is making my life easier but I still hate having to change all my existing code (sour grapes).

Change 3/4 – DON’T Use UpperCamelCase for Properties and Enums, DO Use lowerCamelCase

This change helps drive consistency across your code. All properties and enums should be lower camel case. Eg:

So CGColor becomes cgColor. As a property it should be lower camel case (I was originally a C# dev, and we’re always right about camel casing 😉

The same applies to enums, which brings me to Optionals…which are just an enum a la:

“Some” is now “some” but rather than changing code to the lower case version, why not just dump “some” completely by doing the following:

Change 4/4 – Language Changes, You Know, Like Verbs and Nouns

Apple’s getting all existential on us. “Swift 4 is a language so should behave like one”…That means they’re introducing some kind of tenses. Basically, if the method is mutating then use the imperative. If it does not mutate then use the “ed” or “ing” suffix.

Confusing huh? Let’s have an example from Swift 2 that returns an array to the current variable (“array”):

Then Swift 4:

Simple on the face of it but “sort()” in Swift 2 returned a new sorted array, whereas in Swift 4 the same thing returns a sorted array in place! SO WATCH OUT – go through existing code with a fine tooth comb!


I hope you enjoyed that little snippet of changes to Swift 4. I’ll update this article as and when I find more horrendous code breaking situations!

Changes in Swift 4
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This

Exhausted by your 9 - 5?

Would you prefer to work for half the time and earn double? The first step to your freedom is understanding how apps make money. Simply enter your email below and I will send you a free PDF describing the process.

If you're serious about improving your life then do not miss this opportunity.

You have subscribed! Please check your email to confirm this subscription and to receive your item!