iOS Swift features

Source: Internet
Author: User

Attributes provide more information about declarations and types. There are two types of attributes in Swift that are used to decorate declarations and for decorated types. For example, required an attribute that, when applied to a specified or facilitated initializer declaration for a class, indicates that each of its subclasses must implement that initializer. Another example noreturn of an attribute, when applied to a function or method type, indicates that the function or method does not return to its caller.

Specify an attribute by following the symbol followed by the @ attribute name, or, if the parameter is included, with the parameter:

@attribute name
@attribute name(attribute arguments)

Some declarative attributes specify more information about an attribute by receiving parameters and how it modifies a particular declaration. The parameters of these attributes are written in parentheses, and their format is defined by the attribute they belong to.

declaring attributes

A declaration attribute can only be applied to a declaration. However, you can also noreturn apply an attribute to a function or method type.

assignment

This attribute is used to decorate a function that overloads the compound assignment operator. Functions that overload the compound assignment operator must mark their initial input parameters as inout . assignmentfor an example of how to use an attribute, see: Compound assignment operator.

class_protocol

This feature is used to decorate a protocol that indicates that the protocol can only be used by the class type [to be modified: adopted].

If you objc decorate a protocol with an attribute, the class_protocol attribute is implicitly applied to the protocol, so you do not have to explicitly class_protocol tag the protocol with attributes.

exported

This attribute is used to decorate an import declaration to export a declaration of an imported module, Submodule, or current module. If another module imports the current module, then that module can access the exported items of the current module.

final

This attribute is used to decorate a class or class for properties, methods, and subscript members. If you use it to decorate a class, then this class cannot be inherited. If you use it to decorate a property, method, or subscript in a class, it is represented in a subclass, and they cannot be overridden.

lazy

This attribute is used to decorate a stored-type variable property in a class or struct, indicating that the initial value of the property is computed and stored at most once, and occurs the first time it is accessed. lazyfor an example of how to use attributes, see: Lazy Storage-type properties.

noreturn

The attribute is used to modify a function or method declaration, indicating the corresponding type of the function or method, T Yes @noreturn T . You can use this feature to modify the type of a function or method so that the function or method does not return to its caller.

For a noreturn function or method that is not marked with an attribute, you can override it (override) to mark it with that attribute. Conversely, for a noreturn function or method that has been marked with an attribute, you cannot override it to be marked without using the attribute. The same rules are tried when you implement a protocol method in a comforming type.

NSCopying

This attribute is used to decorate the stored-type variable property of a class. This attribute causes the setter of the property to be composited with a copy of the property value, returned by the copyWithZone method, rather than the value of the property itself. The type of the attribute must follow the NSCopying protocol.

NSCopyingThe behavior of the attribute is similar to that in Objective-c copy .

NSManaged

This attribute is used to decorate a NSManagedObject stored-type variable attribute in a subclass, indicating that the storage and implementation of the attribute is dynamically provided by core data at run time based on the relevant entity description.

objc

This attribute is used to decorate any declaration that can be represented in objective-c, such as non-nested classes, protocols, classes and properties and methods in the Protocol (including Getter and setter), initializer, destructor, subscript below. objcthe Objective-c attribute tells the compiler that the declaration can be used in the code.

If you objc apply an attribute to a class or protocol, it is also implicitly applied to the members of that class or protocol. For classes marked with objc attributes, the compiler implicitly adds attributes to its subclasses objc . A protocol marked with an objc attribute cannot inherit from objc an unmarked protocol.

objcAn attribute has an optional parameter, which consists of a marker. objcYou can use this feature parameter when you want to expose the modified entity to a objective-c with a different name. You can use this parameter to name classes, protocols, methods, Getters,setters, and initializers. The following example ExampleClass exposes the enabled getter in the property to Objective-c, whose name is instead isEnabled of its original property name.

@objcclass ExampleClass {    var enabled: Bool {    @objc(isEnabled) get {        // Return the appropriate value    }    }}

optional

This attribute is used to modify the properties, methods, or subscript members of the Protocol, indicating that implementing these members does not require a consistency type (conforming type).

You can only use optional attributes to modify the protocols that mark the objc attributes. Therefore, only class types can adopt and comform to those protocols that contain optional member requirements. For more guidance on how to use optional features and how to access Optional Protocol members, for example, when you are unsure whether a conforming type implements them, see: Optional protocol requirements.

required

This attribute modifies the specified or convenient initializer of a class, indicating that all subclasses of the class must implement the initializer.

The specified initializer with this attribute must be implemented explicitly, and the convenience initializer can be either explicitly implemented or inherited after the subclass has implemented all the specified initializers for the superclass (or when the subclass uses a handy initializer to override the specified initializer).

Declaration attributes used by Interface Builder

The Interface Builder feature is the declarative feature that Interface Builder uses to synchronize with Xcode. Swift provides the following interface builder features:, IBAction , IBDesignable IBInspectable as well IBOutlet . These properties are conceptually the same as those in objective-c.

IBOutletand IBInspectable a property declaration used to decorate a class; an IBAction attribute is used to decorate a method declaration of a class; a IBDesignable declaration that modifies a class.

Type characteristics

Type attributes can only be used for cosmetic types. However, you can also use noreturn attributes to modify a function or method declaration.

auto_closure

This feature defers the evaluation of an expression by automatically enclosing the expression in a non-parametric closure. Use this attribute to decorate an argument-free function or method type, returning the type of the expression. An example of how to use an auto_closure attribute, see function type

noreturn

This attribute is used to modify the type of a function or method, indicating that the function or method does not return to its caller. You can also use it to mark the declaration of a function or method, indicating the corresponding type of function or method, T Yes @noreturn T .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.