What's new in Macros

Asked on 07/30/2024

1 search

What's New in Macros

At WWDC 2024, Apple introduced several new features and improvements related to macros, particularly in the context of Swift and Xcode.

Swift Macros

  • Previewable Macro: This new macro can be attached to property wrappers like @State, allowing you to use them directly within a preview block. This eliminates the need to write wrapper views, simplifying the code and making it easier to see your UI live with minimal lines of code. For example, you can define your state right in the preview's body and attach the previewable macro, which tells the preview system to create a wrapper view behind the scenes (What’s new in Xcode 16).
  • Preview Modifier: This API makes it easier to share environment or data for previews, reducing duplicate code and enabling the preview system to cache the data (What’s new in Xcode 16).

Swift 6

  • Macros in Swift 6: Swift 6 continues to build on the macro system introduced in previous versions, enhancing portability, performance, and the overall developer experience. Macros help reduce repetitive boilerplate code and enable more expressive APIs (What’s new in Swift).

Xcode Previews

  • Dynamic Linking Architecture: Xcode Previews now use a new dynamic linking architecture that uses the same build artifacts for previews and when you build and run. This avoids the need to rebuild your project when switching between the two, making for a smoother and more productive workflow. The new previewable macro is part of this improvement, allowing dynamic properties like state to be used directly in an Xcode preview (Platforms State of the Union).

Relevant Sessions

  1. What’s new in Xcode 16
  2. What’s new in Swift
  3. Platforms State of the Union

These sessions provide a comprehensive overview of the new features and improvements in macros, particularly in the context of Swift and Xcode.