Published on

attributes

Authors
  • Name
    Twitter

https://doc.swiftgg.team/documentation/the-swift-programming-language-----/attributes/#resultBuilder

中文翻译成特性,我觉得比较恰当。

特性的作用是为声明和类型添加信息。

在Swift中有两种特性:

  1. 适用于声明的特性。
  2. 适用于类型的特性。

特性提供有关声明或类型的附加信息。

例如,函数声明上的discardableResult(可废弃的结果)特性表示,尽管函数返回一个值,但如果返回值未被使用,编译器不应生成警告。

你可以通过编写@符号,并在其后加上特性的名称以及特性接受的任何参数,来指定一个特性。

@<#attribute name#>
@<#attribute name#>(<#attribute arguments>)

有些声明特性接受参数,这些参数指定有关特性的更多信息以及它如何适用于特定声明。这些特性参数被括号括起来,其格式由它们所属的特性定义。

附加宏和属性包装器也使用特性语法。

声明特性

attached

available

backDeployed

discardableResult

dynamicCallable

dynamicMemberLookup

freestanding

frozen

GKInspectable

inlinable

main

nonobjc

NSApplicationMain

NSCopying

NSManaged

objc

objcMembers

preconcurrency

propertyWrapper

resultBuilder

requires_stored_property_inits

testable

UIApplicationMain

unchecked

usableFromInline

warn_unqualified_access

Interface Builder 使用的声明特性

类型特性

autoclosure

convertion

escaping

Sendable

Switch Case 特性

unknown