Notice: Undefined index: understanding-swift in /var/sites/hackingwithswift.com/site/src/Controller/NewBooksReadController.php on line 89
How to check multiple conditions - a free tutorial

NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to check multiple conditions

Paul Hudson    @twostraws   

Swift gives us && and || for checking multiple conditions at the same time, and when used with just two conditions they are fairly straightforward.

As an example, imagine we were running a forum where users could post messages, and delete messages any messages they owned. We might write code like this:

if isOwner == true || isAdmin == true {
    print("You can delete this post")
}

Where things get more confusing is when we want to check several things. For example, we could say that regular users can delete messages only we allowed them, but admins can always delete posts. We might write code like this:

if isOwner == true && isEditingEnabled || isAdmin == true {
    print("You can delete this post")
}

But what is that trying to check? What order are the && and || checks executed? It could mean this:

if (isOwner == true && isEditingEnabled) || isAdmin == true {
    print("You can delete this post")
}

That says “if we’re the owner and editing is enabled you can delete the post, or if you’re an admin you can delete the post even if you don’t own it.” That makes sense: folks can delete their own posts if editing is allowed, but admins can always delete posts.

However, you might also read it like this:

if isOwner == true && (isEditingEnabled || isAdmin == true) {
    print("You can delete this post")
}

And now it means something quite different: “if you’re the owner of the post, and either editing is enabled or you’re the admin, then you can delete the post.” This means admins can’t delete posts they don’t own, which wouldn’t make sense.

Obviously Swift doesn’t like ambiguity like this, so it will always interpret the code as if we had written this:

if (isOwner == true && isEditingEnabled) || isAdmin == true {
    print("You can delete this post")
}

However, honestly it’s not a nice experience to leave this to Swift to figure out, which is why we can insert the parentheses ourselves to clarify exactly what we mean.

There is no specific advice on this, but realistically any time you mix && and || in a single condition you should almost certainly use parentheses to make the result clear.

Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

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

BUY OUR BOOKS
Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Link copied to your pasteboard.