Powerful Features of Swift with Examples
3 min readJul 30, 2024
Swift, Apple’s modern programming language, is designed to make it easier to write and maintain code. It’s known for its performance, safety, and expressive syntax. Let’s explore some of the most powerful features of Swift with examples.
1. Optionals
Optionals handle the absence of a value, a common source of bugs in many programming languages.
var name: String? = "Alison"
if let unwrappedName = name {
print("Hello, \(unwrappedName)!")
} else {
print("Name is nil")
}
This code ensures that unwrappedName
is safely unwrapped before use, preventing runtime errors.
2. Type Inference
Swift can infer the type of a variable or constant, reducing the amount of boilerplate code.