Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ?: operator, formally known as the ternary conditional operator, is a control flow operator that evaluates a boolean expression and returns one of two mutually exclusive expressions based on the result. It is the only ternary operator in Swift, meaning it operates on exactly three operands.
let isReady = true
let status = isReady ? "Go" : "Stop"

Mechanics and Evaluation Rules

1. Boolean Constraint The first operand must evaluate to a strict Bool type. Swift does not support implicit truthiness; passing integers, strings, or optional references directly as the condition results in a compiler error.
let count = 5

// Invalid: 'Int' is not convertible to 'Bool'
// let result = count ? "Yes" : "No" 

// Valid: Explicit boolean expression
let result = count > 0 ? "Positive" : "Negative"
2. Short-Circuit Evaluation The operator employs short-circuit evaluation, meaning only the selected branch is executed.
  • If the condition evaluates to true, the first expression is evaluated and returned. The second expression is completely ignored.
  • If the condition evaluates to false, the second expression is evaluated and returned. The first expression is completely ignored.
func performTrueAction() -> Int { return 1 }
func performFalseAction() -> Int { return 0 }

let flag = true

// performFalseAction() is never executed
let value = flag ? performTrueAction() : performFalseAction()
3. Type Safety and Inference To satisfy Swift’s strict static typing rules, both return expressions must evaluate to the exact same type. Swift does not automatically infer a common supertype (such as a base class or protocol) from two disparate types in a ternary operation. If the two expressions resolve to different types, the Swift compiler emits a type mismatch error. To return a common supertype, you must utilize Swift’s bidirectional type inference by either providing an explicit contextual type for the assignment or explicitly casting at least one of the expressions to the shared supertype.
class Animal {}
class Dog: Animal {}
class Cat: Animal {}

let isDog = true

// Invalid: Result values in '? :' expression have mismatching types 'Dog' and 'Cat'
// let pet = isDog ? Dog() : Cat() 

// Valid Approach 1: Explicit contextual type
let pet: Animal = isDog ? Dog() : Cat()

// Valid Approach 2: Explicit cast on one branch
let anotherPet = isDog ? Dog() as Animal : Cat()
4. Associativity The ternary operator is right-associative. When multiple ternary operators are chained without explicit parentheses, the compiler groups and evaluates the operations from right to left.
let temperature = 25

// Unparenthesized chain
let weather = temperature > 30 ? "Hot" : temperature < 10 ? "Cold" : "Mild"

// How the compiler evaluates the right-associativity
let weatherGrouped = temperature > 30 ? "Hot" : (temperature < 10 ? "Cold" : "Mild")
Master Swift with Deep Grasping Methodology!Learn More