Be Swifty, My Friend 🐥

Last week, I was diving into some properly Swifty stuff with my students. You know that “aha!” moment when Swift’s clean, simple, almost magical syntax starts clicking? Yeah, that. Once you see it, there’s no going back — embracing Swift’s style is a total game-changer.

So, I thought I’d pull together 3 specific Swift syntax tricks that will instantly make your code feel way more "Swifty" — meaning cleaner, shorter, and way easier to read. Let’s jump in!


1. High-Order Functions

High-order functions like map , reduce , and filter help you write expressive code by applying operations across collections without manual loops.

Example: Sum all prices

```swift let prices = [2, 20, 1, 10, 5, 6.99, 4.50, 1, 1, 0.99]

// Swifty way let total = prices.reduce(0, +)

// Traditional way / let total = prices.reduce(0) { result, number in result + number } /

print(total) ```

Example: Lowercase all names

```swift let names = ["Antonio", "Javier", "Aitor", "Gabriel"]

let namesLower = names.map { $0.lowercased() }

print(namesLower) ```


2. Trailing Closures { }

Trailing closures are closures written outside the parentheses of a function call, making code much more readable.

➡️ High-order functions often use trailing closures, like the map example above.

➡️ We also find trailing closures in : - SwiftUI Views - Concurrency (Tasks) - Animations - and more

Example: Trailing closure in concurrency ( Task )

swift Button("Save Task") { Task { do { try await saveTask(title: "Study 2h a day") print("Task saved!") } catch { print("Failed to save the task: \(error)") } } }

Example: Trailing closure in animation

swift withAnimation(.easeInOut(duration: 0.5)) { isExpanded.toggle() }


3. Optional Chaining

Optional chaining lets you access properties, methods, or subscripts on optional values safely , without the need for if let or guard .

Example: Accessing a property through optional chaining

```swift struct Book { var title: String var relatedBooks: [Book]? // Optional list of related books }

func getBook() -> Book? { return Book(title: "The Swift Language Guide") }

let book: Book? = getBook() let bookTitle = book?.title // Optional chaining // If book exists, we access title . Otherwise, bookTitle is nil. ```

Example: Accessing deeply nested optional properties

```swift let related1 = Book(title: "The Swift Language Guide") let related2 = Book(title: "Mastering SwiftUI") let mainBook = Book(title: "Introduction to Swift", relatedBooks: [related1, related2])

let firstRelatedBookTitle = mainBook.relatedBooks?.first?.title ?? "No related books found." print(firstRelatedBookTitle) ```

Without optional chaining, you'd need multiple if let statements. Swift handles it cleanly in one line!


Small syntax changes might seem tiny at first, but they can make a huge difference. Not just in how clean and readable your code looks, but also in how much you actually enjoy writing it. Swift was designed to feel natural, almost like you’re having a conversation with your code. Lean into it! A few little tweaks here and there can turn "okay" code into something that feels smooth, elegant, and so satisfying to build with.

So... 🧘‍♂️ Be Swifty, my friend.

← Back to Home