Property provides more information about declarations and types. There are two types of attributes in Swift, one for declarations and one for types. For example, the required attribute – when used for class designation or to facilitate initialization declarations – specifies that each subclass must implement its initialization function. The noreturn attribute – when applied to the type of a function or method – indicates that the function or method does not return a value.

You can specify a property using the @ character plus the property name and property arguments:

@ Attribute name @ Attribute name (attribute parameter)

Declarative attributes that contain parameters can specify additional information for attributes that can be used for special declarations. These attribute parameters are enclosed in parentheses, and the format of the parameter is determined by the attribute.

Declaration attributes

Declarative properties can only be used for declarative purposes. Of course, you can also use noreturn properties as function or method types.

assignment

This property can be used to modify functions of overloaded compound assignment operators. The function with the overloaded compound assignment operator must mark the initial input parameter with inout. See the compound assignment operator for an example of assignment properties.

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public number: Programming Daxin, whether you are small white or big ox are welcome to enter. Let us progress together and develop together! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)

class_protocol

This property can be used to define class type protocols.

If you use the protocol of the objC attribute, the protocol implicitly contains the class_protocol attribute, and you don’t need to explicitly flag the class_protocol attribute.

exported

This property can be used for internal declarations that expose the internal modules, submodules, or declarations of the current module to other external modules. If another module references the current module, that module can access the exposed parts of the current module.

final

This property can be used to modify a class or a class’s property, method, or member subscript operator. Used of a class to indicate that the class cannot be inherited. When used on a class property, method, or member subscript operator, indicating that these member functions of the class cannot be overridden in any subclass.

lazy

This property can be used to modify a stored variable property in a class or structure, indicating that the property’s initial value can be evaluated and stored at most once the first time it is accessed. For an example of lazy properties, see Lazy storage Properties.

noreturn

This property is used to declare a function or method whose corresponding type T is @noreturn T. You can use this property to modify the type of a function or method when it does not need to return the caller.

You can override functions or methods that do not mark the noreturn attribute. That is, you cannot override functions or methods that have noReturn attributes. Similar rules apply when you implement this type of protocol method.

NSCopying

This property can be used to modify stored variable properties in a class. The modifier’s assignment function consists of a copy of the value of the property – returned by the copyWithZone method – rather than the value of the property itself. This property type must comply with the NSCopying protocol. The NSCopying property is similar to the copy property in Objective-C.

NSManaged

This class inherits from NS-Managed Object, indicating that the storage and implementation of this property is provided dynamically in real time by Core Data based on the associated entity description.

objc

This property can be used in any declaration that can be represented in Objective-C – for example, non-nested classes, protocols, properties and methods of classes and protocols (including value and assignment functions), initializers, destructors, and subscript operators. The objc attribute tells the compiler that this declaration is available in Objective-C code.

If you modify a class or protocol with an objC attribute, it explicitly applies to all members of the class or protocol. When a class inherits from another class that annotates objC attributes, the compiler explicitly adds objC attributes to the class. Protocols that annotate objC attributes cannot inherit from protocols that do not contain objC attributes.

Objc attributes can accept a single attribute parameter consisting of identifiers. You can use objC properties when the part you want to expose to Objective-C is a different name. You can use this parameter to name classes, protocols, methods, value functions, assignment functions, and initializers. The following example is a function that evaluates the enabled property of ExampleClass, which exposes isEnabled to Objective-C code rather than the property’s original name.


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

optional

This property can be used for protocol properties, methods, or member subscript operators to indicate that implementations of member functions of this type are not required.

The optional attribute can only be used for protocols that annotate objC attributes. Therefore, protocols that contain optional members apply only to class types. For more guidance on how to use the optional attribute and how to access optional protocol members – for example, if you are not sure if they implement this type – see Optional protocol Requirements.

required

When used to specify a class or to facilitate an initialization function, this property indicates that every subclass of the class must implement the initialization function.

The specified initialization function of the requirement must be explicitly included. When a subclass directly implements all of the specified initializer functions of the superclass (or when a subclass overrides the specified initializer using the convenience initializer function), the required convenience initializer must be explicitly included or inherited.

Declare properties using Interface Builder

The Interface Builder property uses Interface Builder to declare properties to synchronize with Xcode. Swift provides the following Interface Builder properties: IBAction, IBdesignable, IBInspectable, and IBOutlet. These properties are theoretically the same as their objective-C counterparts.

The IBOutlet and IBInspectable properties are used for class property declarations, the IBAction property is used for class method declarations, and the IBDesignable property is used for class declarations.

The type attribute

Type attributes can be used only for types. Of course, the noreturn attribute can also be used to declare functions or methods.

auto_closure

This property is used to delay the evaluation of an expression’s assignment, automatically encapsulating the expression into a parameterless closure. This property can also be used as the type of a function or method that takes no arguments and returns an expression type. See function type for an example of the auto_closure property.

noreturn

When used for a function or method, this property indicates that the function or method has no return value. You can also mark the declaration of a function or method with this property to indicate that the corresponding type T of the function or method is @noreturn T.

Attribute syntax attribute → @attribute-name attribute-argument-clause opt attribute-name → identifier attribute-argument-clause → (balanced-token opt) Attributes → Attribute Attributes opt balanced-tokens -tokens opt Balanced-tokens → (balanced-tokens opt) balanced-tokens → [balanced-tokens opt] balanced-tokens → {balanced-tokens opt} Balanced-token → Any identifier, keyword, constant, or operator balanced-token → Any punctuation (,), [,], {, or}