iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🏟️

Swift '@...' Quiz: Where Do They Come From?

に公開

When writing code in Swift, you may sometimes use expressions starting with the @ symbol, such as @〜〜〜. Do you know exactly what that @〜〜〜 is and where it came from?

This article is for Day 20 of the Swift Advent Calendar 2024.

Swift's @〜〜〜

In Swift, the @〜〜〜 syntax is used to indicate the following five things:

  • "Attributes" in the Swift language
    • Types of attributes
      • Declaration attributes
      • Type attributes
      • Switch case attributes
  • "Property wrappers," a feature of the Swift language
  • "Result builders," a feature of the Swift language
  • "Global actors," a feature of the Swift language
  • "Attached macros," a feature of the Swift language

For example, when developing iOS applications using Swift, you might use things like @main, @State, @ViewBuilder, @MainActor, and @Model. All of these can be classified into one of the five categories above. Furthermore, if it is a "property wrapper," "result builder," "global actor," or "attached macro," it is defined within some framework.

Examples

Q0-1. What is @main and where does it come from?

Answer for Q0-1. @main
@main is an "attribute (declaration attribute)" defined in the "Swift language itself."

Q0-2. What is @State and where does it come from?

Answer for Q0-2. @State
@State is a "property wrapper" defined in the "SwiftUI framework."

Q0-3. What is @ViewBuilder and where does it come from?

Answer for Q0-3. @ViewBuilder
@ViewBuilder is a "result builder" defined in the "SwiftUI framework."

Q0-4. What is @MainActor and where does it come from?

Answer for Q0-4. @MainActor
@MainActor is a "global actor" defined in the "Swift language (Swift Standard Library)."

Q0-5. What is @Model and where does it come from?

Answer for Q0-5. @Model
@Model is an "attached macro" defined in the "SwiftData framework."

Quiz - Beginner

Q1-1. What is @EnvironmentObject and where does it come from?

Answer for Q1-1. @EnvironmentObject

@EnvironmentObject is a "property wrapper" defined in the "SwiftUI framework."

Q1-2. What is @Binding and where does it come from?

Answer for Q1-2. @Binding

@Binding is a "property wrapper" defined in the "SwiftUI framework."

Q1-3. What is @Published and where does it come from?

Answer for Q1-3. @Published

@Published is a "property wrapper" defined in the "Combine framework."
(However, in environments where the "Combine framework" can be used, there is also a typealias definition in the "Foundation framework.")

Q1-4. What is @Observable and where does it come from?

Answer for Q1-4. @Observable

@Observable is an "attached macro" defined in the "Swift language (Observation framework)."

Q1-5. What is @SceneBuilder and where does it come from?

Answer for Q1-5. @SceneBuilder

@SceneBuilder is a "result builder" defined in the "SwiftUI framework."

Q1-6. What is @AppStorage and where does it come from?

Answer for Q1-6. @AppStorage

@AppStorage is a "property wrapper" defined in the "SwiftUI framework."

Q1-7. What is @Test and where does it come from?

Answer for Q1-7. @Test

@Test is an "attached macro" defined in the "Swift language (Testing framework)."

Q1-8. What is @testable and where does it come from?

Answer for Q1-8. @testable

@testable is an "attribute (declaration attribute)" defined in the "Swift language itself."

Q1-9. What is @escaping and where does it come from?

Answer for Q1-9. @escaping

@escaping is an "attribute (type attribute)" defined in the "Swift language itself."

Q1-10. What is @UIApplicationMain and where does it come from?

Answer for Q1-10. @UIApplicationMain

@UIApplicationMain is an "attribute (declaration attribute)" defined in the "Swift language itself."

Quiz - Intermediate

Q2-1. What is @Bindable and where does it come from?

Answer for Q2-1. @Bindable

@Bindable is a "property wrapper" defined in the "SwiftUI framework."

Q2-2. What is @Environment and where does it come from?

Answer for Q2-2. @Environment

@Environment is a "property wrapper" defined in the "SwiftUI framework."

Q2-3. What is @Previewable and where does it come from?

Answer for Q2-3. @Previewable

@Previewable is an "attached macro" defined in the "SwiftUI framework."

Q2-4. What is @Query and where does it come from?

Answer for Q2-4. @Query

@Query is an "attached macro" defined in the "SwiftData framework."

Q2-5. What is @MapContentBuilder and where does it come from?

Answer for Q2-5. @MapContentBuilder

@MapContentBuilder is a "result builder" defined in the "MapKit framework and SwiftUI framework."

Q2-6. What is @UIApplicationDelegateAdaptor and where does it come from?

Answer for Q2-6. @UIApplicationDelegateAdaptor

@UIApplicationDelegateAdaptor is a "property wrapper" defined in the "SwiftUI framework."

Q2-7. What is @Suite and where does it come from?

Answer for Q2-7. @Suite

@Suite is an "attached macro" defined in the "Swift language (Testing framework)."

Q2-8. What is @unchecked and where does it come from?

Answer for Q2-8. @unchecked

@unchecked is an "attribute (declaration attribute)" defined in the "Swift language itself."

Q2-9. What is @Sendable and where does it come from?

Answer for Q2-9. @Sendable

@Sendable is an "attribute (type attribute)" defined in the "Swift language itself."

Q2-10. What is @preconcurrency and where does it come from?

Answer for Q2-10. @preconcurrency

@preconcurrency is an "attribute (declaration attribute)" defined in the "Swift language itself."

Quiz - Advanced

Q3-1. What is @Entry and where does it come from?

Answer for Q3-1. @Entry

@Entry is an "attached macro" defined in the "SwiftUI framework."

Q3-2. What is @unknown and where does it come from?

Answer for Q3-2. @unknown

@unknown is an "attribute (switch case attribute)" defined in the "Swift language itself."

Q3-3. What is @Tag and where does it come from?

Answer for Q3-3. @Tag

@Tag is an "attached macro" defined in the "Swift language (Testing framework)."

Q3-4. What is @ModelActor and where does it come from?

Answer for Q3-4. @ModelActor

@ModelActor is an "attached macro" defined in the "SwiftData framework."

Q3-5. What is @ViewLoading and where does it come from?

Answer for Q3-5. @ViewLoading

@ViewLoading is a "property wrapper" defined in the "AppKit framework" or "UIKit framework."

Q3-6. What is @propertyWrapper and where does it come from?

Answer for Q3-6. @propertyWrapper

@propertyWrapper is an "attribute (declaration attribute)" defined in the "Swift language itself."

Q3-7. What is @CodableConfiguration and where does it come from?

Answer for Q3-7. @CodableConfiguration

@CodableConfiguration is a "property wrapper" defined in the "Swift language (Foundation)."

Q3-8. What is @resultBuilder and where does it come from?

Answer for Q3-8. @resultBuilder

@resultBuilder is an "attribute (declaration attribute)" defined in the "Swift language itself."

Q3-9. What is @TransferRepresentationBuilder and where does it come from?

Answer for Q3-9. @TransferRepresentationBuilder

@TransferRepresentationBuilder is a "result builder" defined in the "Core Transferable framework."

Q3-10. What is @TaskLocal and where does it come from?

Answer for Q3-10. @TaskLocal

@TaskLocal is defined in the "Swift language"; it was a "property wrapper" before Swift 6.0, and has been an "attached macro" since Swift 6.0.

Answer List / Reference Links

Answer List / Reference Links

Examples

0-1. @main

Syntax From
Attribute (declaration attribute) Swift Language itself

0-2. @State

Syntax From
Property wrapper SwiftUI framework

0-3. @ViewBuilder

Syntax From
Result builder SwiftUI framework

0-4. @MainActor

Syntax From
Global actor Swift language (Swift Standard Library)

0-5. @Model

Syntax From
Attached macro SwiftData framework

Beginner

1-1. @EnvironmentObject

Syntax From
Property wrapper SwiftUI framework

1-2. @Binding

Syntax From
Property wrapper SwiftUI framework

1-3. @Published

Syntax From
Property wrapper Combine framework
(typealias definition for the above) (Foundation framework)

1-4. @Observable

Syntax From
Attached macro Swift language (Observation framework)

1-5. @SceneBuilder

Syntax From
Result builder SwiftUI framework

1-6. @AppStorage

Syntax From
Property wrapper SwiftUI framework

1-7. @Test

Syntax From
Attached macro Swift language (Testing framework)

1-8. @testable

Syntax From
Attribute (declaration attribute) Swift language itself

1-9. @escaping

Syntax From
Attribute (type attribute) Swift language itself

1-10. @UIApplicationMain

Syntax From
Attribute (declaration attribute) Swift language itself

Intermediate

2-1. @Bindable

Syntax From
Property wrapper SwiftUI framework

2-2. @Environment

Syntax From
Property wrapper SwiftUI framework

2-3. @Previewable

Syntax From
Attached macro SwiftUI framework

2-4. @Query

Syntax From
Attached macro SwiftData framework

2-5. @MapContentBuilder

Syntax From
Result builder MapKit framework and SwiftUI framework

2-6. @UIApplicationDelegateAdaptor

Syntax From
Property wrapper SwiftUI framework

2-7. @Suite

Syntax From
Attached macro Swift language (Testing framework)

2-8. @unchecked

Syntax From
Attribute (declaration attribute) Swift language itself

2-9. @Sendable

Syntax From
Attribute (type attribute) Swift language itself

2-10. @preconcurrency

Syntax From
Attribute (declaration attribute) Swift language itself

Advanced

3-1. @Entry

Syntax From
Attached macro SwiftUI framework

3-2. @unknown

Syntax From
Attribute (switch case attribute) Swift language itself

3-3. @Tag

Syntax From
Attached macro Swift language (Testing framework)

3-4. @ModelActor

Syntax From
Attached macro SwiftData framework

3-5. @ViewLoading

Syntax From
Property wrapper AppKit framework
Property wrapper UIKit framework

3-6. @propertyWrapper

Syntax From
Attribute (declaration attribute) Swift language itself

3-7. @CodableConfiguration

Syntax From
Property wrapper Swift language (Foundation)

3-8. @resultBuilder

Syntax From
Attribute (declaration attribute) Swift language itself

3-9. @TransferRepresentationBuilder

Syntax From
Result builder Core Transferable framework

3-10. @TaskLocal

Syntax From
Before Swift 6.0: Property wrapper Swift language
From Swift 6.0: Attached macro Swift language

Share Your Results!

How many questions did you get right? Post your results on social media.

Below is the text for copying and pasting into your post ↓

I got n questions right on the "Swift '@〜〜〜' Origins Quiz"!

https://zenn.dev/treastrain/articles/7110008404105c #zenn #QiitaAdventCalendar

Discussion