Boolean Methods and Toggling
Use toggle() and Boolean-returning methods.
Boolean Methods and Toggling is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)Frequently asked questions
Is the “Boolean Methods and Toggling” lesson free?
Yes — the full text of “Boolean Methods and Toggling” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- Bool Values and Expressions
- The Ternary Conditional Operator
- Avoiding Nested Ternaries
- Boolean Methods and Toggling