Attributes

Attributes

There are two kinds of attributes in Swift—those that apply to declarations and those that apply to types. An attribute provides additional information about the declaration or type. For example, the discardableResult attribute on a function declaration indicates that, although the function returns a value, the compiler shouldn’t generate a warning if the return value is unused.

You specify an attribute by writing the @ symbol followed by the attribute’s name and any arguments that the attribute accepts:

  1. @attribute name
  2. @attribute name(attribute arguments)

Some declaration attributes accept arguments that specify more information about the attribute and how it applies to a particular declaration. These attribute arguments are enclosed in parentheses, and their format is defined by the attribute they belong to.

Declaration Attributes

You can apply a declaration attribute to declarations only.

available

Apply this attribute to indicate a declaration’s life cycle relative to certain Swift language versions or certain platforms and operating system versions.

The available attribute always appears with a list of two or more comma-separated attribute arguments. These arguments begin with one of the following platform or language names:

  • iOS

  • iOSApplicationExtension

  • macOS

  • macOSApplicationExtension

  • macCatalyst

  • macCatalystApplicationExtension

  • watchOS

  • watchOSApplicationExtension

  • tvOS

  • tvOSApplicationExtension

  • swift

You can also use an asterisk (*) to indicate the availability of the declaration on all of the platform names listed above. An available attribute that specifies availability using a Swift version number can’t use the asterisk.

The remaining arguments can appear in any order and specify additional information about the declaration’s life cycle, including important milestones.

  • The unavailable argument indicates that the declaration isn’t available on the specified platform. This argument can’t be used when specifying Swift version availability.

  • The introduced argument indicates the first version of the specified platform or language in which the declaration was introduced. It has the following form:

    1. introduced: version number

    The version number consists of one to three positive integers, separated by periods.

  • The deprecated argument indicates the first version of the specified platform or language in which the declaration was deprecated. It has the following form:

    1. deprecated: version number

    The optional version number consists of one to three positive integers, separated by periods. Omitting the version number indicates that the declaration is currently deprecated, without giving any information about when the deprecation occurred. If you omit the version number, omit the colon (:) as well.

  • The obsoleted argument indicates the first version of the specified platform or language in which the declaration was obsoleted. When a declaration is obsoleted, it’s removed from the specified platform or language and can no longer be used. It has the following form:

    1. obsoleted: version number

    The version number consists of one to three positive integers, separated by periods.

  • The message argument provides a textual message that the compiler displays when emitting a warning or error about the use of a deprecated or obsoleted declaration. It has the following form:

    1. message: message

    The message consists of a string literal.

  • The renamed argument provides a textual message that indicates the new name for a declaration that’s been renamed. The compiler displays the new name when emitting an error about the use of a renamed declaration. It has the following form:

    1. renamed: new name

    The new name consists of a string literal.

    You can apply the available attribute with the renamed and unavailable arguments to a type alias declaration, as shown below, to indicate that the name of a declaration changed between releases of a framework or library. This combination results in a compile-time error that the declaration has been renamed.

    1. // First release
    2. protocol MyProtocol {
    3. // protocol definition
    4. }
    1. // Subsequent release renames MyProtocol
    2. protocol MyRenamedProtocol {
    3. // protocol definition
    4. }
    5. @available(*, unavailable, renamed: "MyRenamedProtocol")
    6. typealias MyProtocol = MyRenamedProtocol

You can apply multiple available attributes on a single declaration to specify the declaration’s availability on different platforms and different versions of Swift. The declaration that the available attribute applies to is ignored if the attribute specifies a platform or language version that doesn’t match the current target. If you use multiple available attributes, the effective availability is the combination of the platform and Swift availabilities.

If an available attribute only specifies an introduced argument in addition to a platform or language name argument, you can use the following shorthand syntax instead:

  1. @available(platform name version number, *)
  2. @available(swift version number)

The shorthand syntax for available attributes concisely expresses availability for multiple platforms. Although the two forms are functionally equivalent, the shorthand form is preferred whenever possible.

  1. @available(iOS 10.0, macOS 10.12, *)
  2. class MyClass {
  3. // class definition
  4. }

An available attribute that specifies availability using a Swift version number can’t additionally specify a declaration’s platform availability. Instead, use separate available attributes to specify a Swift version availability and one or more platform availabilities.

  1. @available(swift 3.0.2)
  2. @available(macOS 10.12, *)
  3. struct MyStruct {
  4. // struct definition
  5. }

discardableResult

Apply this attribute to a function or method declaration to suppress the compiler warning when the function or method that returns a value is called without using its result.

dynamicCallable

Apply this attribute to a class, structure, enumeration, or protocol to treat instances of the type as callable functions. The type must implement either a dynamicallyCall(withArguments:) method, a dynamicallyCall(withKeywordArguments:) method, or both.

You can call an instance of a dynamically callable type as if it’s a function that takes any number of arguments.

  1. @dynamicCallable
  2. struct TelephoneExchange {
  3. func dynamicallyCall(withArguments phoneNumber: [Int]) {
  4. if phoneNumber == [4, 1, 1] {
  5. print("Get Swift help on forums.swift.org")
  6. } else {
  7. print("Unrecognized number")
  8. }
  9. }
  10. }
  11. let dial = TelephoneExchange()
  12. // Use a dynamic method call.
  13. dial(4, 1, 1)
  14. // Prints "Get Swift help on forums.swift.org"
  15. dial(8, 6, 7, 5, 3, 0, 9)
  16. // Prints "Unrecognized number"
  17. // Call the underlying method directly.
  18. dial.dynamicallyCall(withArguments: [4, 1, 1])

The declaration of the dynamicallyCall(withArguments:) method must have a single parameter that conforms to the ExpressibleByArrayLiteral [https://developer.apple.com/documentation/swift/expressiblebyarrayliteral\] protocol—like [Int] in the example above. The return type can be any type.

You can include labels in a dynamic method call if you implement the dynamicallyCall(withKeywordArguments:) method.

  1. @dynamicCallable
  2. struct Repeater {
  3. func dynamicallyCall(withKeywordArguments pairs: KeyValuePairs<String, Int>) -> String {
  4. return pairs
  5. .map { label, count in
  6. repeatElement(label, count: count).joined(separator: " ")
  7. }
  8. .joined(separator: "\n")
  9. }
  10. }
  11. let repeatLabels = Repeater()
  12. print(repeatLabels(a: 1, b: 2, c: 3, b: 2, a: 1))
  13. // a
  14. // b b
  15. // c c c
  16. // b b
  17. // a

The declaration of the dynamicallyCall(withKeywordArguments:) method must have a single parameter that conforms to the ExpressibleByDictionaryLiteral [https://developer.apple.com/documentation/swift/expressiblebydictionaryliteral\] protocol, and the return type can be any type. The parameter’s Key [https://developer.apple.com/documentation/swift/expressiblebydictionaryliteral/2294108-key\] must be ExpressibleByStringLiteral [https://developer.apple.com/documentation/swift/expressiblebystringliteral\]. The previous example uses KeyValuePairs [https://developer.apple.com/documentation/swift/keyvaluepairs\] as the parameter type so that callers can include duplicate parameter labels—a and b appear multiple times in the call to repeat.

If you implement both dynamicallyCall methods, dynamicallyCall(withKeywordArguments:) is called when the method call includes keyword arguments. In all other cases, dynamicallyCall(withArguments:) is called.

You can only call a dynamically callable instance with arguments and a return value that match the types you specify in one of your dynamicallyCall method implementations. The call in the following example doesn’t compile because there isn’t an implementation of dynamicallyCall(withArguments:) that takes KeyValuePairs<String, String>.

  1. repeatLabels(a: "four") // Error

dynamicMemberLookup

Apply this attribute to a class, structure, enumeration, or protocol to enable members to be looked up by name at runtime. The type must implement a subscript(dynamicMember:) subscript.

In an explicit member expression, if there isn’t a corresponding declaration for the named member, the expression is understood as a call to the type’s subscript(dynamicMember:) subscript, passing information about the member as the argument. The subscript can accept a parameter that’s either a key path or a member name; if you implement both subscripts, the subscript that takes key path argument is used.

An implementation of subscript(dynamicMember:) can accept key paths using an argument of type KeyPath [https://developer.apple.com/documentation/swift/keypath\], WritableKeyPath [https://developer.apple.com/documentation/swift/writablekeypath\], or ReferenceWritableKeyPath [https://developer.apple.com/documentation/swift/referencewritablekeypath\]. It can accept member names using an argument of a type that conforms to the ExpressibleByStringLiteral [https://developer.apple.com/documentation/swift/expressiblebystringliteral\] protocol—in most cases, String. The subscript’s return type can be any type.

Dynamic member lookup by member name can be used to create a wrapper type around data that can’t be type checked at compile time, such as when bridging data from other languages into Swift. For example:

  1. @dynamicMemberLookup
  2. struct DynamicStruct {
  3. let dictionary = ["someDynamicMember": 325,
  4. "someOtherMember": 787]
  5. subscript(dynamicMember member: String) -> Int {
  6. return dictionary[member] ?? 1054
  7. }
  8. }
  9. let s = DynamicStruct()
  10. // Use dynamic member lookup.
  11. let dynamic = s.someDynamicMember
  12. print(dynamic)
  13. // Prints "325"
  14. // Call the underlying subscript directly.
  15. let equivalent = s[dynamicMember: "someDynamicMember"]
  16. print(dynamic == equivalent)
  17. // Prints "true"

Dynamic member lookup by key path can be used to implement a wrapper type in a way that supports compile-time type checking. For example:

  1. struct Point { var x, y: Int }
  2. @dynamicMemberLookup
  3. struct PassthroughWrapper<Value> {
  4. var value: Value
  5. subscript<T>(dynamicMember member: KeyPath<Value, T>) -> T {
  6. get { return value[keyPath: member] }
  7. }
  8. }
  9. let point = Point(x: 381, y: 431)
  10. let wrapper = PassthroughWrapper(value: point)
  11. print(wrapper.x)

frozen

Apply this attribute to a structure or enumeration declaration to restrict the kinds of changes you can make to the type. This attribute is allowed only when compiling in library evolution mode. Future versions of the library can’t change the declaration by adding, removing, or reordering an enumeration’s cases or a structure’s stored instance properties. These changes are allowed on nonfrozen types, but they break ABI compatibility for frozen types.

Note

When the compiler isn’t in library evolution mode, all structures and enumerations are implicitly frozen, and this attribute is ignored.

In library evolution mode, code that interacts with members of nonfrozen structures and enumerations is compiled in a way that allows it to continue working without recompiling even if a future version of the library adds, removes, or reorders some of that type’s members. The compiler makes this possible using techniques like looking up information at runtime and adding a layer of indirection. Marking a structure or enumeration as frozen gives up this flexibility to gain performance: Future versions of the library can make only limited changes to the type, but the compiler can make additional optimizations in code that interacts with the type’s members.

Frozen types, the types of the stored properties of frozen structures, and the associated values of frozen enumeration cases must be public or marked with the usableFromInline attribute. The properties of a frozen structure can’t have property observers, and expressions that provide the initial value for stored instance properties must follow the same restrictions as inlinable functions, as discussed in inlinable.

To enable library evolution mode on the command line, pass the -enable-library-evolution option to the Swift compiler. To enable it in Xcode, set the “Build Libraries for Distribution” build setting (BUILD_LIBRARY_FOR_DISTRIBUTION) to Yes, as described in Xcode Help [https://help.apple.com/xcode/mac/current/#/dev04b3a04ba\].

A switch statement over a frozen enumeration doesn’t require a default case, as discussed in Switching Over Future Enumeration Cases. Including a default or @unknown default case when switching over a frozen enumeration produces a warning because that code is never executed.

GKInspectable

Apply this attribute to expose a custom GameplayKit component property to the SpriteKit editor UI. Applying this attribute also implies the objc attribute.

inlinable

Apply this attribute to a function, method, computed property, subscript, convenience initializer, or deinitializer declaration to expose that declaration’s implementation as part of the module’s public interface. The compiler is allowed to replace calls to an inlinable symbol with a copy of the symbol’s implementation at the call site.

Inlinable code can interact with public symbols declared in any module, and it can interact with internal symbols declared in the same module that are marked with the usableFromInline attribute. Inlinable code can’t interact with private or fileprivate symbols.

This attribute can’t be applied to declarations that are nested inside functions or to fileprivate or private declarations. Functions and closures that are defined inside an inlinable function are implicitly inlinable, even though they can’t be marked with this attribute.

main

Apply this attribute to a structure, class, or enumeration declaration to indicate that it contains the top-level entry point for program flow. The type must provide a main type function that doesn’t take any arguments and returns Void. For example:

  1. @main
  2. struct MyTopLevel {
  3. static func main() {
  4. // Top-level code goes here
  5. }
  6. }

Another way to describe the requirements of the main attribute is that the type you write this attribute on must satisfy the same requirements as types that conform to the following hypothetical protocol:

  1. protocol ProvidesMain {
  2. static func main() throws
  3. }

The Swift code you compile to make an executable can contain at most one top-level entry point, as discussed in Top-Level Code.

nonobjc

Apply this attribute to a method, property, subscript, or initializer declaration to suppress an implicit objc attribute. The nonobjc attribute tells the compiler to make the declaration unavailable in Objective-C code, even though it’s possible to represent it in Objective-C.

Applying this attribute to an extension has the same effect as applying it to every member of that extension that isn’t explicitly marked with the objc attribute.

You use the nonobjc attribute to resolve circularity for bridging methods in a class marked with the objc attribute, and to allow overloading of methods and initializers in a class marked with the objc attribute.

A method marked with the nonobjc attribute can’t override a method marked with the objc attribute. However, a method marked with the objc attribute can override a method marked with the nonobjc attribute. Similarly, a method marked with the nonobjc attribute can’t satisfy a protocol requirement for a method marked with the objc attribute.

NSApplicationMain

Apply this attribute to a class to indicate that it’s the application delegate. Using this attribute is equivalent to calling the NSApplicationMain(_:_:) function.

If you don’t use this attribute, supply a main.swift file with code at the top level that calls the NSApplicationMain(_:_:) function as follows:

  1. import AppKit
  2. NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

The Swift code you compile to make an executable can contain at most one top-level entry point, as discussed in Top-Level Code.

NSCopying

Apply this attribute to a stored variable property of a class. This attribute causes the property’s setter to be synthesized with a copy of the property’s value—returned by the copyWithZone(_:) method—instead of the value of the property itself. The type of the property must conform to the NSCopying protocol.

The NSCopying attribute behaves in a way similar to the Objective-C copy property attribute.

NSManaged

Apply this attribute to an instance method or stored variable property of a class that inherits from NSManagedObject to indicate that Core Data dynamically provides its implementation at runtime, based on the associated entity description. For a property marked with the NSManaged attribute, Core Data also provides the storage at runtime. Applying this attribute also implies the objc attribute.

objc

Apply this attribute to any declaration that can be represented in Objective-C—for example, nonnested classes, protocols, nongeneric enumerations (constrained to integer raw-value types), properties and methods (including getters and setters) of classes, protocols and optional members of a protocol, initializers, and subscripts. The objc attribute tells the compiler that a declaration is available to use in Objective-C code.

Applying this attribute to an extension has the same effect as applying it to every member of that extension that isn’t explicitly marked with the nonobjc attribute.

The compiler implicitly adds the objc attribute to subclasses of any class defined in Objective-C. However, the subclass must not be generic, and must not inherit from any generic classes. You can explicitly add the objc attribute to a subclass that meets these criteria, to specify its Objective-C name as discussed below. Protocols that are marked with the objc attribute can’t inherit from protocols that aren’t marked with this attribute.

The objc attribute is also implicitly added in the following cases:

  • The declaration is an override in a subclass, and the superclass’s declaration has the objc attribute.

  • The declaration satisfies a requirement from a protocol that has the objc attribute.

  • The declaration has the IBAction, IBSegueAction, IBOutlet, IBDesignable, IBInspectable, NSManaged, or GKInspectable attribute.

If you apply the objc attribute to an enumeration, each enumeration case is exposed to Objective-C code as the concatenation of the enumeration name and the case name. The first letter of the case name is capitalized. For example, a case named venus in a Swift Planet enumeration is exposed to Objective-C code as a case named PlanetVenus.

The objc attribute optionally accepts a single attribute argument, which consists of an identifier. The identifier specifies the name to be exposed to Objective-C for the entity that the objc attribute applies to. You can use this argument to name classes, enumerations, enumeration cases, protocols, methods, getters, setters, and initializers. If you specify the Objective-C name for a class, protocol, or enumeration, include a three-letter prefix on the name, as described in Conventions [https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html#//apple\_ref/doc/uid/TP40011210-CH10-SW1\] in Programming with Objective-C [https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html#//apple\_ref/doc/uid/TP40011210\]. The example below exposes the getter for the enabled property of the ExampleClass to Objective-C code as isEnabled rather than just as the name of the property itself.

  1. class ExampleClass: NSObject {
  2. @objc var enabled: Bool {
  3. @objc(isEnabled) get {
  4. // Return the appropriate value
  5. }
  6. }
  7. }

For more information, see Importing Swift into Objective-C [https://developer.apple.com/documentation/swift/imported\_c\_and\_objective-c\_apis/importing\_swift\_into\_objective-c\].

Note

The argument to the objc attribute can also change the runtime name for that declaration. You use the runtime name when calling functions that interact with the Objective-C runtime, like NSClassFromString [https://developer.apple.com/documentation/foundation/1395135-nsclassfromstring\], and when specifying class names in an app’s Info.plist file. If you specify a name by passing an argument, that name is used as the name in Objective-C code and as the runtime name. If you omit the argument, the name used in Objective-C code matches the name in Swift code, and the runtime name follows the normal Swift compiler convention of name mangling.

objcMembers

Apply this attribute to a class declaration, to implicitly apply the objc attribute to all Objective-C compatible members of the class, its extensions, its subclasses, and all of the extensions of its subclasses.

Most code should use the objc attribute instead, to expose only the declarations that are needed. If you need to expose many declarations, you can group them in an extension that has the objc attribute. The objcMembers attribute is a convenience for libraries that make heavy use of the introspection facilities of the Objective-C runtime. Applying the objc attribute when it isn’t needed can increase your binary size and adversely affect performance.

propertyWrapper

Apply this attribute to a class, structure, or enumeration declaration to use that type as a property wrapper. When you apply this attribute to a type, you create a custom attribute with the same name as the type. Apply that new attribute to a property of a class, structure, or enumeration to wrap access to the property through an instance of the wrapper type; apply the attribute to a local stored variable declaration to wrap access to the variable the same way. Computed variables, global variables, and constants can’t use property wrappers.

The wrapper must define a wrappedValue instance property. The wrapped value of the property is the value that the getter and setter for this property expose. In most cases, wrappedValue is a computed value, but it can be a stored value instead. The wrapper defines and manages any underlying storage needed by its wrapped value. The compiler synthesizes storage for the instance of the wrapper type by prefixing the name of the wrapped property with an underscore (_)—for example, the wrapper for someProperty is stored as _someProperty. The synthesized storage for the wrapper has an access control level of private.

A property that has a property wrapper can include willSet and didSet blocks, but it can’t override the compiler-synthesized get or set blocks.

Swift provides two forms of syntactic sugar for initialization of a property wrapper. You can use assignment syntax in the definition of a wrapped value to pass the expression on the right-hand side of the assignment as the argument to the wrappedValue parameter of the property wrapper’s initializer. You can also provide arguments to the attribute when you apply it to a property, and those arguments are passed to the property wrapper’s initializer. For example, in the code below, SomeStruct calls each of the initializers that SomeWrapper defines.

  1. @propertyWrapper
  2. struct SomeWrapper {
  3. var wrappedValue: Int
  4. var someValue: Double
  5. init() {
  6. self.wrappedValue = 100
  7. self.someValue = 12.3
  8. }
  9. init(wrappedValue: Int) {
  10. self.wrappedValue = wrappedValue
  11. self.someValue = 45.6
  12. }
  13. init(wrappedValue value: Int, custom: Double) {
  14. self.wrappedValue = value
  15. self.someValue = custom
  16. }
  17. }
  18. struct SomeStruct {
  19. // Uses init()
  20. @SomeWrapper var a: Int
  21. // Uses init(wrappedValue:)
  22. @SomeWrapper var b = 10
  23. // Both use init(wrappedValue:custom:)
  24. @SomeWrapper(custom: 98.7) var c = 30
  25. @SomeWrapper(wrappedValue: 30, custom: 98.7) var d
  26. }

The projected value for a wrapped property is a second value that a property wrapper can use to expose additional functionality. The author of a property wrapper type is responsible for determining the meaning of its projected value and defining the interface that the projected value exposes. To project a value from a property wrapper, define a projectedValue instance property on the wrapper type. The compiler synthesizes an identifier for the projected value by prefixing the name of the wrapped property with a dollar sign ($)—for example, the projected value for someProperty is $someProperty. The projected value has the same access control level as the original wrapped property.

  1. @propertyWrapper
  2. struct WrapperWithProjection {
  3. var wrappedValue: Int
  4. var projectedValue: SomeProjection {
  5. return SomeProjection(wrapper: self)
  6. }
  7. }
  8. struct SomeProjection {
  9. var wrapper: WrapperWithProjection
  10. }
  11. struct SomeStruct {
  12. @WrapperWithProjection var x = 123
  13. }
  14. let s = SomeStruct()
  15. s.x // Int value
  16. s.$x // SomeProjection value
  17. s.$x.wrapper // WrapperWithProjection value

resultBuilder

Apply this attribute to a class, structure, enumeration to use that type as a result builder. A result builder is a type that builds a nested data structure step by step. You use result builders to implement a domain-specific language (DSL) for creating nested data structures in a natural, declarative way. For an example of how to use the resultBuilder attribute, see Result Builders.

Result-Building Methods

A result builder implements static methods described below. Because all of the result builder’s functionality is exposed through static methods, you don’t ever initialize an instance of that type. The buildBlock(_:) method is required; the other methods—which enable additional functionality in the DSL—are optional. The declaration of a result builder type doesn’t actually have to include any protocol conformance.

The description of the static methods uses three types as placeholders. The type Expression is a placeholder for the type of the result builder’s input, Component is a placeholder for the type of a partial result, and FinalResult is a placeholder for the type of the result that the result builder produces. You replace these types with the actual types that your result builder uses. If your result-building methods don’t specify a type for Expression or FinalResult, they default to being the same as Component.

The result-building methods are as follows:

static func buildBlock(_ components: Component...) -> Component

Combines an array of partial results into a single partial result. A result builder must implement this method.

static func buildOptional(_ component: Component?) -> Component

Builds a partial result from a partial result that can be nil. Implement this method to support if statements that don’t include an else clause.

static func buildEither(first: Component) -> Component

Builds a partial result whose value varies depending on some condition. Implement both this method and buildEither(second:) to support switch statements and if statements that include an else clause.

static func buildEither(second: Component) -> Component

Builds a partial result whose value varies depending on some condition. Implement both this method and buildEither(first:) to support switch statements and if statements that include an else clause.

static func buildArray(_ components: [Component]) -> Component

Builds a partial result from an array of partial results. Implement this method to support for loops.

static func buildExpression(_ expression: Expression) -> Component

Builds a partial result from an expression. You can implement this method to perform preprocessing—for example, converting expressions to an internal type—or to provide additional information for type inference at use sites.

static func buildFinalResult(_ component: Component) -> FinalResult

Builds a final result from a partial result. You can implement this method as part of a result builder that uses a different type for partial and final results, or to perform other postprocessing on a result before returning it.

static func buildLimitedAvailability(_ component: Component) -> Component

Builds a partial result that propagates or erases type information outside a compiler-control statement that performs an availability check. You can use this to erase type information that varies between the conditional branches.

For example, the code below defines a simple result builder that builds an array of integers. This code defines Compontent and Expression as type aliases, to make it easier to match the examples below to the list of methods above.

  1. @resultBuilder
  2. struct ArrayBuilder {
  3. typealias Component = [Int]
  4. typealias Expression = Int
  5. static func buildExpression(_ element: Expression) -> Component {
  6. return [element]
  7. }
  8. static func buildOptional(_ component: Component?) -> Component {
  9. guard let component = component else { return [] }
  10. return component
  11. }
  12. static func buildEither(first component: Component) -> Component {
  13. return component
  14. }
  15. static func buildEither(second component: Component) -> Component {
  16. return component
  17. }
  18. static func buildArray(_ components: [Component]) -> Component {
  19. return Array(components.joined())
  20. }
  21. static func buildBlock(_ components: Component...) -> Component {
  22. return Array(components.joined())
  23. }
  24. }

Result Transformations

The following syntactic transformations are applied recursively to turn code that uses result-builder syntax into code that calls the static methods of the result builder type:

  • If the result builder has a buildExpression(_:) method, each expression becomes a call to that method. This transformation is always first. For example, the following declarations are equivalent:

    1. @ArrayBuilder var builderNumber: [Int] { 10 }
    2. var manualNumber = ArrayBuilder.buildExpression(10)
  • An assignment statement is transformed like an expression, but is understood to evaluate to (). You can define an overload of buildExpression(_:) that takes an argument of type () to handle assignments specifically.

  • A branch statement that checks an availability condition becomes a call to the buildLimitedAvailability(_:) method. This transformation happens before the transformation into a call to buildEither(first:), buildEither(second:), or buildOptional(_:). You use the buildLimitedAvailability(_:) method to erase type information that changes depending on which branch is taken. For example, the buildEither(first:) and buildEither(second:) methods below use a generic type that captures type information about both branches.

    1. protocol Drawable {
    2. func draw() -> String
    3. }
    4. struct Text: Drawable {
    5. var content: String
    6. init(_ content: String) { self.content = content }
    7. func draw() -> String { return content }
    8. }
    9. struct Line<D: Drawable>: Drawable {
    10. var elements: [D]
    11. func draw() -> String {
    12. return elements.map { $0.draw() }.joined(separator: "")
    13. }
    14. }
    15. struct DrawEither<First: Drawable, Second: Drawable>: Drawable {
    16. var content: Drawable
    17. func draw() -> String { return content.draw() }
    18. }
    19. @resultBuilder
    20. struct DrawingBuilder {
    21. static func buildBlock<D: Drawable>(_ components: D...) -> Line<D> {
    22. return Line(elements: components)
    23. }
    24. static func buildEither<First, Second>(first: First)
    25. -> DrawEither<First, Second> {
    26. return DrawEither(content: first)
    27. }
    28. static func buildEither<First, Second>(second: Second)
    29. -> DrawEither<First, Second> {
    30. return DrawEither(content: second)
    31. }
    32. }

    However, this approach causes a problem in code that has availability checks:

    1. @available(macOS 99, *)
    2. struct FutureText: Drawable {
    3. var content: String
    4. init(_ content: String) { self.content = content }
    5. func draw() -> String { return content }
    6. }
    7. @DrawingBuilder var brokenDrawing: Drawable {
    8. if #available(macOS 99, *) {
    9. FutureText("Inside.future") // Problem
    10. } else {
    11. Text("Inside.present")
    12. }
    13. }
    14. // The type of brokenDrawing is Line<DrawEither<Line<FutureText>, Line<Text>>>

    In the code above, FutureText appears as part of the type of brokenDrawing because it’s one of the types in the DrawEither generic type. This could cause your program to crash if FutureText isn’t available at runtime, even in the case where that type is explicitly not being used.

    To solve this problem, implement a buildLimitedAvailability(_:) method to erase type information. For example, the code below builds an AnyDrawable value from its availability check.

    1. struct AnyDrawable: Drawable {
    2. var content: Drawable
    3. func draw() -> String { return content.draw() }
    4. }
    5. extension DrawingBuilder {
    6. static func buildLimitedAvailability(_ content: Drawable) -> AnyDrawable {
    7. return AnyDrawable(content: content)
    8. }
    9. }
    10. @DrawingBuilder var typeErasedDrawing: Drawable {
    11. if #available(macOS 99, *) {
    12. FutureText("Inside.future")
    13. } else {
    14. Text("Inside.present")
    15. }
    16. }
    17. // The type of typeErasedDrawing is Line<DrawEither<AnyDrawable, Line<Text>>>
  • A branch statement becomes a series of nested calls to the buildEither(first:) and buildEither(second:) methods. The statements’ conditions and cases are mapped onto the leaf nodes of a binary tree, and the statement becomes a nested call to the buildEither methods following the path to that leaf node from the root node.

    For example, if you write a switch statement that has three cases, the compiler uses a binary tree with three leaf nodes. Likewise, because the path from the root node to the second case is “second child” and then “first child”, that case becomes a nested call like buildEither(first: buildEither(second: ... )). The following declarations are equivalent:

    1. let someNumber = 19
    2. @ArrayBuilder var builderConditional: [Int] {
    3. if someNumber < 12 {
    4. 31
    5. } else if someNumber == 19 {
    6. 32
    7. } else {
    8. 33
    9. }
    10. }
    11. var manualConditional: [Int]
    12. if someNumber < 12 {
    13. let partialResult = ArrayBuilder.buildExpression(31)
    14. let outerPartialResult = ArrayBuilder.buildEither(first: partialResult)
    15. manualConditional = ArrayBuilder.buildEither(first: outerPartialResult)
    16. } else if someNumber == 19 {
    17. let partialResult = ArrayBuilder.buildExpression(32)
    18. let outerPartialResult = ArrayBuilder.buildEither(second: partialResult)
    19. manualConditional = ArrayBuilder.buildEither(first: outerPartialResult)
    20. } else {
    21. let partialResult = ArrayBuilder.buildExpression(33)
    22. manualConditional = ArrayBuilder.buildEither(second: partialResult)
    23. }
  • A branch statement that might not produce a value, like an if statement without an else clause, becomes a call to buildOptional(_:). If the if statement’s condition is satisfied, its code block is transformed and passed as the argument; otherwise, buildOptional(_:) is called with nil as its argument. For example, the following declarations are equivalent:

    1. @ArrayBuilder var builderOptional: [Int] {
    2. if (someNumber % 2) == 1 { 20 }
    3. }
    4. var partialResult: [Int]? = nil
    5. if (someNumber % 2) == 1 {
    6. partialResult = ArrayBuilder.buildExpression(20)
    7. }
    8. var manualOptional = ArrayBuilder.buildOptional(partialResult)
  • A code block or do statement becomes a call to the buildBlock(_:) method. Each of the statements inside of the block is transformed, one at a time, and they become the arguments to the buildBlock(_:) method. For example, the following declarations are equivalent:

    1. @ArrayBuilder var builderBlock: [Int] {
    2. 100
    3. 200
    4. 300
    5. }
    6. var manualBlock = ArrayBuilder.buildBlock(
    7. ArrayBuilder.buildExpression(100),
    8. ArrayBuilder.buildExpression(200),
    9. ArrayBuilder.buildExpression(300)
    10. )
  • A for loop becomes a temporary variable, a for loop, and call to the buildArray(_:) method. The new for loop iterates over the sequence and appends each partial result to that array. The temporary array is passed as the argument in the buildArray(_:) call. For example, the following declarations are equivalent:

    1. @ArrayBuilder var builderArray: [Int] {
    2. for i in 5...7 {
    3. 100 + i
    4. }
    5. }
    6. var temporary: [[Int]] = []
    7. for i in 5...7 {
    8. let partialResult = ArrayBuilder.buildExpression(100 + i)
    9. temporary.append(partialResult)
    10. }
    11. let manualArray = ArrayBuilder.buildArray(temporary)
  • If the result builder has a buildFinalResult(_:) method, the final result becomes a call to that method. This transformation is always last.

Although the transformation behavior is described in terms of temporary variables, using a result builder doesn’t actually create any new declarations that are visible from the rest of your code.

You can’t use break, continue, defer, guard, or return statements, while statements, or do-catch statements in the code that a result builder transforms.

The transformation process doesn’t change declarations in the code, which lets you use temporary constants and variables to build up expressions piece by piece. It also doesn’t change throw statements, compile-time diagnostic statements, or closures that contain a return statement.

Whenever possible, transformations are coalesced. For example, the expression 4 + 5 * 6 becomes buildExpression(4 + 5 * 6) rather multiple calls to that function. Likewise, nested branch statements become a single binary tree of calls to the buildEither methods.

Custom Result-Builder Attributes

Creating a result builder type creates a custom attribute with the same name. You can apply that attribute in the following places:

  • On a function declaration, the result builder builds the body of the function.

  • On a variable or subscript declaration that includes a getter, the result builder builds the body of the getter.

  • On a parameter in a function declaration, the result builder builds the body of a closure that’s passed as the corresponding argument.

Applying a result builder attribute doesn’t impact ABI compatibility. Applying a result builder attribute to a parameter makes that attribute part of the function’s interface, which can effect source compatibility.

requires_stored_property_inits

Apply this attribute to a class declaration to require all stored properties within the class to provide default values as part of their definitions. This attribute is inferred for any class that inherits from NSManagedObject.

testable

Apply this attribute to an import declaration to import that module with changes to its access control that simplify testing the module’s code. Entities in the imported module that are marked with the internal access-level modifier are imported as if they were declared with the public access-level modifier. Classes and class members that are marked with the internal or public access-level modifier are imported as if they were declared with the open access-level modifier. The imported module must be compiled with testing enabled.

UIApplicationMain

Apply this attribute to a class to indicate that it’s the application delegate. Using this attribute is equivalent to calling the UIApplicationMain function and passing this class’s name as the name of the delegate class.

If you don’t use this attribute, supply a main.swift file with code at the top level that calls the UIApplicationMain(_:_:_:_:) [https://developer.apple.com/documentation/uikit/1622933-uiapplicationmain\] function. For example, if your app uses a custom subclass of UIApplication as its principal class, call the UIApplicationMain(_:_:_:_:) function instead of using this attribute.

The Swift code you compile to make an executable can contain at most one top-level entry point, as discussed in Top-Level Code.

usableFromInline

Apply this attribute to a function, method, computed property, subscript, initializer, or deinitializer declaration to allow that symbol to be used in inlinable code that’s defined in the same module as the declaration. The declaration must have the internal access level modifier. A structure or class marked usableFromInline can use only types that are public or usableFromInline for its properties. An enumeration marked usableFromInline can use only types that are public or usableFromInline for the raw values and associated values of its cases.

Like the public access level modifier, this attribute exposes the declaration as part of the module’s public interface. Unlike public, the compiler doesn’t allow declarations marked with usableFromInline to be referenced by name in code outside the module, even though the declaration’s symbol is exported. However, code outside the module might still be able to interact with the declaration’s symbol by using runtime behavior.

Declarations marked with the inlinable attribute are implicitly usable from inlinable code. Although either inlinable or usableFromInline can be applied to internal declarations, applying both attributes is an error.

warn_unqualified_access

Apply this attribute to a top-level function, instance method, or class or static method to trigger warnings when that function or method is used without a preceding qualifier, such as a module name, type name, or instance variable or constant. Use this attribute to help discourage ambiguity between functions with the same name that are accessible from the same scope.

For example, the Swift standard library includes both a top-level min(_:_:) [https://developer.apple.com/documentation/swift/1538339-min/\] function and a min() [https://developer.apple.com/documentation/swift/sequence/1641174-min\] method for sequences with comparable elements. The sequence method is declared with the warn_unqualified_access attribute to help reduce confusion when attempting to use one or the other from within a Sequence extension.

Declaration Attributes Used by Interface Builder

Interface Builder attributes are declaration attributes used by Interface Builder to synchronize with Xcode. Swift provides the following Interface Builder attributes: IBAction, IBSegueAction, IBOutlet, IBDesignable, and IBInspectable. These attributes are conceptually the same as their Objective-C counterparts.

You apply the IBOutlet and IBInspectable attributes to property declarations of a class. You apply the IBAction and IBSegueAction attribute to method declarations of a class and the IBDesignable attribute to class declarations.

Applying the IBAction, IBSegueAction, IBOutlet, IBDesignable, or IBInspectable attribute also implies the objc attribute.

Type Attributes

You can apply type attributes to types only.

autoclosure

Apply this attribute to delay the evaluation of an expression by automatically wrapping that expression in a closure with no arguments. You apply it to a parameter’s type in a function or method declaration, for a parameter whose type is a function type that takes no arguments and that returns a value of the type of the expression. For an example of how to use the autoclosure attribute, see Autoclosures and Function Type.

convention

Apply this attribute to the type of a function to indicate its calling conventions.

The convention attribute always appears with one of the following arguments:

  • The swift argument indicates a Swift function reference. This is the standard calling convention for function values in Swift.

  • The block argument indicates an Objective-C compatible block reference. The function value is represented as a reference to the block object, which is an id-compatible Objective-C object that embeds its invocation function within the object. The invocation function uses the C calling convention.

  • The c argument indicates a C function reference. The function value carries no context and uses the C calling convention.

With a few exceptions, a function of any calling convention can be used when a function any other calling convention is needed. A nongeneric global function, a local function that doesn’t capture any local variables, or a closure that doesn’t capture any local variables can be converted to the C calling convention. Other Swift functions can’t be converted to the C calling convention. A function with the Objective-C block calling convention can’t be converted to the C calling convention.

escaping

Apply this attribute to a parameter’s type in a function or method declaration to indicate that the parameter’s value can be stored for later execution. This means that the value is allowed to outlive the lifetime of the call. Function type parameters with the escaping type attribute require explicit use of self. for properties or methods. For an example of how to use the escaping attribute, see Escaping Closures.

Switch Case Attributes

You can apply switch case attributes to switch cases only.

unknown

Apply this attribute to a switch case to indicate that it isn’t expected to be matched by any case of the enumeration that’s known at the time the code is compiled. For an example of how to use the unknown attribute, see Switching Over Future Enumeration Cases.

Grammar of an attribute

attribute → @ attribute-name attribute-argument-clause opt

attribute-name → identifier

attribute-argument-clause → ( balanced-tokens opt )

attributes → attribute attributes opt

balanced-tokens → balanced-token balanced-tokens opt

balanced-token → ( balanced-tokens opt )

balanced-token → [ balanced-tokens opt ]

balanced-token → { balanced-tokens opt }

balanced-token → Any identifier, keyword, literal, or operator

balanced-token → Any punctuation except (, ), [, ], {, or }