O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Boolean type

Boolean values are often referred to as logical values because they can be either true or false. Swift has a built-in Boolean type that accepts one of the two built-in Boolean constants: true and false.

Boolean constants and variables can be defined like this:

let swiftIsCool = true  
var itIsRaining = false 

Boolean values are especially useful when working with conditional statements, such as the if, while, and guard statements. For example, what do you think this code would do?

let isSwiftCool = true  
let isItRaining = false  
if isSwiftCool { 
  print("YEA, I cannot wait to learn it") 
} 
if isItRaining {  
  print("Get a rain coat") 
} 

If you answered that this code would print out YEA, I cannot wait to learn it, then you would be ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required