Top 10 Swift 5.7 Syntax Sugar

3 minute read

Published:

Swift has continually evolved to make code more concise and readable without compromising performance. The latest Swift version 5.7 introduces several syntactic sugar features that make coding in Swift even more delightful. Here are the top 10 syntax sugar features with examples:

1. Implicit Returns from Single-Expression Functions

In Swift 5.7, you can omit the return keyword for single-expression functions, closures, and computed properties.

// Before
func square(number: Int) -> Int {
    return number * number
}

// Now
func square(number: Int) -> Int {
    number * number
}

2. Multiple Trailing Closures

Swift allows for a cleaner syntax when passing multiple trailing closures, improving readability.

// Before
UIView.animate(withDuration: 0.3, animations: {
    // animations
}, completion: { finished in
    // completion
})

// Now
UIView.animate(withDuration: 0.3) {
    // animations
} completion: { finished in
    // completion
}

3. Synthesized Memberwise Initializers for Structs

Swift 5.7 enhances synthesized memberwise initializers for structs, allowing default values for properties.

struct User {
    var name: String
    var age: Int = 18
}

let user = User(name: "Alice")

4. Simplified Optional Handling with guard let and if let

Swift simplifies optional binding by allowing shadowing of the unwrapped variable.

// Before
if let foo = foo {
    // use foo
}

// Now
if let foo {
    // use foo
}

5. Concise switch Statement with Multiple Cases

let value = 5
switch value {
case 1, 2, 3:
    print("Value is 1, 2, or 3")
case 4, 5, 6:
    print("Value is 4, 5, or 6")
default:
    print("Value is something else")
}

6. @resultBuilder for Building DSLs

Swift’s @resultBuilder attribute simplifies the creation of domain-specific languages (DSLs).

@resultBuilder
struct StringBuilder {
    static func buildBlock(_ components: String...) -> String {
        components.joined(separator: " ")
    }
}

@StringBuilder
func makeSentence() -> String {
    "Swift"
    "is"
    "awesome!"
}

let sentence = makeSentence() // "Swift is awesome!"

7. Key Path Expressions as Functions

Key path expressions can be used as functions for more concise code.

struct Person {
    let name: String
}

let people = [Person(name: "Alice"), Person(name: "Bob")]
let names = people.map(\.name)

8. Enum Case Pattern Matching

Swift allows for concise enum case pattern matching in if and guard statements.

enum Result {
    case success
    case failure(Error)
}

let result: Result = .success

if case .success = result {
    print("Success!")
}

9. Syntactic Sugar for Function Types

Swift 5.7 introduces a more concise syntax for function types with parameter labels.

// Before
let add: (Int, Int) -> Int = { (a: Int, b: Int) in
    return a + b
}
// Now
let add: (Int, Int) -> Int = { a, b in
    a + b
}

10. Property Wrappers

Property wrappers provide a convenient way to reuse logic for setting and getting properties.

@propertyWrapper
struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
}

struct Person {
    @Capitalized var name: String
}

var person = Person()
person.name = "sheldon wang"
print(person.name) // "Sheldon Wang"