Here are my 5 favorite Swift tips 🤌
Hi 👋
Last week has been very exciting for me! I’ve released a video with an amazing returning guest, and also my very first paid content!
The reception for both has been very positive and I wanted to thank you all for your support 🙌
This week I will be bringing back a format I haven’t done in quite some time: my live newsletter! As usual, it will happen on Thursday, 8PM CET.
I’ve realized that I’ve been sharing tips to help iOS developers write better code for something like five years!
Over that time I’ve shared a good amount of tips, covering very different topics.
So I figured it would be interesting if I were to go over that (long) list and pick my top 5 tips, the ones that I believe are really worth knowing!
#01 – The Modern API of UIButton
If you enjoy writing Swift code as much as I do, you’re probably a bit sad every time you need to use mechanisms that have been inherited from Objective-C.
Like passing an #selector() to register the action of a UIButton.
But did you know that since iOS 14 you can use a more modern and swiftier API instead?
#02 – XCTest + try!
When you’re writing a test that needs to call a throwing function, it’s very easy to just use try! and call it a day.
However this approach has a big drawback: if the function does throw an error, then your testing process will crash and subsequent tests won’t be executed.
As it turns out, there’s no need to use try!: instead you can just mark you test as throwing and XCTest will take care of making the test fail when an error is thrown 👌
#03 – XCTUnwrap vs Force Unwrap
Talking about tests, another time when a bad practice can easily creep in is when you need to deal with an Optional value.
It’s, once again, very easy to quickly solve the issue by force unwrapping the value. But this has the same down side as we’ve seen above: a nil value will result in a crash of the testing process.
You might be happy to learn that XCTest introduced a great tool for this situation!
By using the function XCTUnwrap(), you get the same simplicity than with a force unwrap, except that now a nil value will result in an error being thrown instead of the testing process crashing 😎
#04 – Multiline String
If you’ve ever needed to write a literal String with many line breaks, you’ve probably noticed how the many “\n” characters quickly degrade the readability of the code.
There’s actually a better way to write such a String in Swift: by using the dedicated multiline String syntax!
#05 – Large Number Separators
To stay on the topic of literal values and code readability, if you’ve ever needed to write a large numerical constant, you’ve probably noticed how passed 5 or 6 digits the value becomes hard to read at a glance.
This is issue is actually extremely simple to solve: in any numerical literal, Swift allows you to add “_” inside the constant, to better separate digits:
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!
❤️