Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
137: Debugging Macro Expansion
Macros are incredibly powerful, but they come with a specific kind of frustration: when they break, the compiler usually yells at the line where you applied the macro, not the line in your macro implementation where you actually messed up the syntax. It's like being told your house is on fire, but the fire is actually in a hidden crawlspace you didn't know existed.
I've seen this a dozen times. You're building a @Logged macro to wrap function bodies in print statements, you hit Cmd+B, and suddenly you get a generic Expected '}' in closure or Consecutive declarations on a line must be separated by ';'. The error points to your class definition, but your class definition looks perfect. The bug is hiding in the generated code.
The Mystery of the Vanishing Brace
Take a look at this snippet. I'm trying to build a @PropertyWrapperLog macro that generates a helper method to print the value of a property. It's supposed to look like this:
struct User {
@PropertyWrapperLog var name: String = "Alice"
// Macro should generate: func logName() { print("name: \(name)") }
}
But instead, I'm getting this compiler error pointing directly at the @PropertyWrapperLog line:
"Expected declaration".
If I look at my macro implementation, I'm manually constructing the DeclSyntax. Here is the culprit:
// Inside the Macro implementation
let methodName = "log\(capitalizedName)()"
return [
"func \(methodName) { print(\"\(propertyName): \\(self.\(propertyName))\")"
// Wait... where is the closing brace?
]
In a standard Swift file, forgetting a closing brace is obvious. But inside a macro, you're essentially writing code that writes code. The compiler expands the macro, injects the broken string into your source, and then the result fails to compile. Because the source of the injection is the attribute, that's where the error marker lands. It's a total guessing game if you don't know how to see the expanded result.
Peeking Under the Hood with Expand Macro
You should almost never try to debug macro syntax by "guessing" what the string looks like. Xcode has a built-in tool specifically for this.
Right-click on the macro attribute (e.g., @PropertyWrapperLog) in your source code. In the menu, you'll see Expand Macro. When you click this, Xcode replaces the attribute with the actual code the macro generated. It's a temporary view, but it's the only way to see exactly what your Syntax trees are producing.
When I did this for the example above, I saw the generated code: func logName() { print("name: \(self.name)")
The missing } became immediately obvious. The "Expected declaration" error was simply the compiler saying, "I started reading a function, but I hit the end of the struct before the function ever finished."
Fixing the Syntax Tree Leak
The fix is simple—add the brace—but the better fix is to stop relying on raw strings for complex expansions. I usually prefer using SwiftSyntax's specialized types or, at the very least, multi-line string literals to make the structure visible to my own eyes.
// The fixed, more readable version
let body = "print(\"\(propertyName): \\(self.\(propertyName))\")"
return [
"""
func log\(capitalizedName)() {
\(body)
}
"""
]
Now, when I rebuild, the error vanishes. I've verified it by expanding the macro again, and the braces are exactly where they need to be. I'll leave you with this: if you see a syntax error on a line that looks visually perfect, 99% of the time, a macro on that line is generating "garbage" code. Expand it first, then fix the implementation.
📋 Practical Task
Repairing the Broken @Coordinate Macro
You are working on a project that uses a @Coordinate macro. This macro is intended to be placed on a struct to automatically generate a method called distance(to: x, y:) that calculates the distance between two points.
However, the project is currently failing to compile. The error "Consecutive declarations on a line must be separated by ';'" is appearing on the struct declaration, even though the struct looks correct.
Your Task:
- Analyze the provided macro implementation below.
- Identify the syntax error being generated in the
distancemethod expansion. - Correct the string interpolation so that the generated method is syntactically valid Swift.
// BUGGY MACRO IMPLEMENTATION
public struct CoordinateMacro: MemberMacro {
public static func expansion(
of node: SomeMacroExpansion,
in context: SomeMacroExpansionContext
) throws -> [DeclSyntax] {
return [
"""
func distance(toX x: Double, toY y: Double) -> Double {
sqrt(pow(x - self.x, 2) + pow(y - self.y, 2)
}
"""
]
}
}There are no comments for now.