Booleans and Swift

Become a Subscriber

Booleans are a type in Swift that is particularly useful in control flow. Booleans can only ever be true or false and we can write statements to execute code based on the value of a Boolean . The Boolean type in Swift is simply written Bool.

Let’s declare a variable of type Bool and set it to true:

 var isFileTooBig : Bool = true

The importance of Booleans will come a bit later on when we discuss conditionals and if statements. For example, I could write something like:

if isFileTooBig {
    // Reject the file for uploading
} else {
    // Begin Uploading a File

This statement is a going to run different blocks of code depending on if the Boolean isFileTooBig is true or false. If isFileTooBig is true, we could write a function to reject the file a user is trying to upload. Else we begin uploading the file.