UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Key points

I hope you can recognize that Swift takes code safety seriously. This takes a number of forms, of which the most important ones are:

  1. All your variables and constants must have a specific type, like Int or String. There is a special Any type, used when you want to mix data together, but even then you’ll need to typecast values back to Int or whatever in order to use them.
  2. If you create a function that says it will return a value, Swift will ensure it always does, no matter what route is taken through its code.
  3. Optionals ensure you always work with real values. Swift won’t let you touch optionals if they are nil, unless you specifically force override it – there’s a reason ! is sometimes called “the crash operator”.
  4. When you use a switch/case block, it must be exhaustive – it must cover all possible cases.

Let’s take another look at some optional code. Consider this:

var a: Int? = 5
var b: Int? = 10

That creates two variables, a and b, both of which store optional integers. That means they might contain a number like 0, 10, 2309832, or they also be nil – they might have no value at all. How would you add those two numbers together?

Remember: Swift doesn’t let you use optionals directly; you need to unwrap them safely first. This is because they can be nil, and “5 + nil” is undefined behavior that used to crash your app or produce weird behavior before Swift came along with its optionals.

In this case, we can unwrap a and b using if let. This creates new constants that are regular integers rather than optional, so we can work with them directly:

if let unwrappedA = a {
    if let unwrappedB = b {
        let c = unwrappedA + unwrappedB
    }
}

That let c line will only be reached if both a and b had a value, so Swift knows it’s safe to run.

Swift lets you merge those two if let statements into one, like this:

if let unwrappedA = a, let unwrappedB = b {
    let c = unwrappedA + unwrappedB
}

While that’s common practice, I prefer to avoid it in this book because it’s a bit harder to read.

Another important take-away is that Swift is a modern programming language. It used many of the latest development techniques when it launched, as has since evolved further. This means we benefit from great features like optionals, closures, switch/case ranges, and more.

Finally, I hope you feel like you’re able to go back to the playground now and start writing a little of your own code. Sure, there’s no iOS code yet, but you can still have a go at attempting the challenge below.

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.