Ternary in Swift

Find this useful? Support us: Star on GitHub 6
Category: Conditional | Language: Swift

In Swift, the ternary operator ? : is a shorthand way of writing an if-else statement. It's usually used for simple checks and simple operations. The basic syntax of the ternary operator is condition ? true expression : false expression.

Here's an example:

let age = 18
let status: String

if age >= 18 {
status = "adult"
} else {
status = "minor"
}

This code can be simplified using the ternary operator:

let age = 18
let status = age >= 18 ? "adult" : "minor"

Another example could be checking if a variable is nil:

let name: String? = "John"
let greeting = "Hello, " + (name ?? "anonymous user") + "!"

Here, if name is not nil, greeting will be "Hello, John!". If name is nil, the ternary operator ?? will provide the default value "anonymous user". Thus, greeting will be "Hello, anonymous user!".

In summary, the ternary operator ? : is a useful shorthand for simple if-else statements in Swift.