swift 註解 (和java比照)@attribute name
阿新 • • 發佈:2018-11-14
Attributes provide more information about a declaration or type. There are two kinds of attributes in Swift, those that apply to declarations and those that apply to types.
You specify an attribute by writing the @
symbol followed by the attribute’s name and any arguments that the attribute accepts:
- @attribute name
- @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.
- @dynamicMemberLookup
- struct DynamicStruct {
- let dictionary = ["someDynamicMember": 325,
- "someOtherMember": 787]
- subscript(dynamicMember member: String) -> Int {
- return dictionary[member] ?? 1054
- }
- }
- let s = DynamicStruct()
- // Using dynamic member lookup
- let dynamic = s.someDynamicMember
- print(dynamic)
- // Prints "325"
- // Calling the underlying subscript directly
- let equivalent = s[dynamicMember: "someDynamicMember"]
- print(dynamic == equivalent)
https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#grammar_attribute-argument-clause