Swift Academy · Lesson

Boolean Methods and Toggling

Use toggle() and Boolean-returning methods.

Lesson 4 of 413 steps

Boolean Methods and Toggling is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Bool Has Methods Too

In Swift even a Bool is a type with useful methods. The most common is toggle(), which flips the value in place.

var isOn = false
isOn.toggle()
print(isOn)

Toggle vs Manual Flip

Before toggle() existed, people wrote isOn = !isOn. The method version is shorter and avoids repeating the variable name.

var darkMode = true
darkMode.toggle()
print(darkMode)
darkMode.toggle()
print(darkMode)

Toggle Requires var

Because toggle() mutates the value, the Bool must be a var. Calling it on a let is a compile error.

var flagged = false
for _ in 1...3 {
    flagged.toggle()
}
print(flagged)

Bool-Returning Methods

Many types expose Bool-returning members. isEmpty on a String tells you whether it has no characters.

let name = ""
print(name.isEmpty)
let city = "Paris"
print(city.isEmpty)

isEmpty on Collections

Arrays and dictionaries also have isEmpty. It is clearer and faster than comparing count to zero.

let scores: [Int] = []
print(scores.isEmpty)
let items = [1, 2, 3]
print(items.isEmpty)

contains Returns a Bool

The contains method checks whether a collection holds a given element, returning true or false.

let fruits = ["apple", "pear", "kiwi"]
print(fruits.contains("pear"))
print(fruits.contains("mango"))

String Prefix and Suffix

Strings offer hasPrefix and hasSuffix, both returning Bool. They are handy for simple text checks.

let file = "report.pdf"
print(file.hasSuffix(".pdf"))
print(file.hasPrefix("report"))

Using Bool Methods in if

Because these methods return a Bool, they slot directly into an if condition.

let input = ""
if input.isEmpty {
    print("Please enter a value")
} else {
    print("Thanks")
}

Negating a Bool Method

Combine ! with a Bool method to express "not". For example, "if the list is not empty".

let cart = ["book"]
if !cart.isEmpty {
    print("Ready to checkout")
}

allSatisfy

The allSatisfy method returns true only if every element passes a test you provide.

let ages = [20, 34, 19, 41]
let allAdults = ages.allSatisfy { $0 >= 18 }
print(allAdults)

Combining Bool Results

You can join several Bool-returning calls with logical operators to form a single decision.

let email = "user@example.com"
let valid = !email.isEmpty && email.contains("@")
print(valid)

Quick Check

Test Bool methods.

Recap: Boolean Methods and Toggling

Use toggle() to flip a Bool var in place, and lean on Bool-returning members like isEmpty, contains, hasPrefix, and allSatisfy to write expressive conditions.

var visible = false
visible.toggle()
let tags = ["new"]
print(visible && !tags.isEmpty)
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Boolean Methods and Toggling” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Boolean Methods and Toggling”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Boolean Methods and Toggling”?

Use toggle() and Boolean-returning methods. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Boolean Methods and Toggling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Bool Values and Expressions
  2. The Ternary Conditional Operator
  3. Avoiding Nested Ternaries
  4. Boolean Methods and Toggling
← Back to Swift Academy