Here are 3 advanced tips in Swift 💡


Hi 👋

WWDC is right around the corner! (and thanks to Photoroom I’m actually attending it onsite at Apple Park today!)

Now before we learn about all the new exciting additions to iOS and Swift, how about discovering 3 advanced tips in Swift that you can already use?

But first, I have a big thank you for the sponsor of this email: Runway


Advertisement

Runway is the only mobile release management platform.
Mobile releases don’t have to be duct-taped processes that you dread tackling.

Stop experiencing death by a thousand branch cuts. Use Runway instead.

👉 Learn how we can help 👈


Sponsors like Runway really help me grow my content creation, so if you have time please make sure to check out their survey: it’s a direct support to my content creation ☺️


A few weeks ago I released a video with Natalia Panferova as a guest, a former Apple engineer who’s just released a new book focused on advanced tips and tricks in Swift.

In the video Natalia shared 3 super interesting advanced features of Swift and here’s a quick recap of each one of them!

#01 – Iterate over items and indices

You might have already used the method .enumerated().

And maybe that you’ve written that kind of code:

If that’s the case, you’ll be interested to learn that calling the first variable index is actually incorrect and dangerous!

Because this value is not guaranteed to always contain a valid index that can be used to safely subscript the array, and trying to use it as such can make your app crash.

So I definitely recommend that you check out this chapter of the video to learn about two safer alternatives 😌

#02 – Labelling loop statements to control execution of nested loops

If you often write code that requires dealing with several nested loops while you iterate over complex data structures, you’re gonna enjoy this tip!

As it turns out, Swift supports giving a label to a loop statement, which in turns allows us to refer to that specific loop in a break statement:

And in the example above, using this labelling feature allows us to reduce the number of code paths and local variables that we need to manage.

#03 – Overloading the pattern matching operator

Pattern matching a value through a switch statement is quite a known trick in Swift, however what is lesser known is the fact that it’s possible implement your own custom pattern matching rules.

For that all you need is to overload the pattern matching operator ~= and you’ll be able to switch over any user-defined type:

Even better, you’ll see in the video an example of how you can overload this operator more than once, which allows to try and match values of different types inside a same switch statement!


I hope that you’ve enjoyed discovering these 3 advanced tips in Swift!

And don’t forget that if you want to listen to the full explanation for each of these tips, you have it available in the YouTube video:


That’s all for this email, thanks for reading it!

If you’ve enjoyed it, feel free to forward it
to your friends and colleagues 🙌

I wish you an amazing week!

❤️

Previous
Previous

My highlights of WWDC 2024 as an iOS developer 🍎

Next
Next

Previews in Xcode also work with UIKit!