Swift Learning: extension (Extensions)

Source: Internet
Author: User

An extension is the addition of new functionality for an existing class, struct, enumeration type, or protocol type. This includes the ability to extend a type without having permission to get the original source code (that is, reverse modeling). Extensions are similar to OBJECTIVE-C classifications. (unlike Objective-c, Swift's extension has no name.) )

The extensions in Swift can:

Adding computed and computed type properties

Defining instance methods and type methods

To provide a new constructor

Define Subscript

Defining and using a new nested type

Make an existing type conform to a protocol

In Swift, you can even extend the Protocol to provide the implementation of the Protocol requirements, or add additional functionality so that the types that meet the protocol have these capabilities. You can get more details from the protocol extension.

Attention

Extensions can add new functionality to a type, but cannot override existing functionality.

Extended syntax

Use the key sub-extension to declare the extension:

Extension SomeType {

New features added for SomeType write here

}

You can extend an existing type by extending it to adopt one or more protocols. In this case, the protocol name is written in the same way, whether it is a class or a struct.

Extension Sometype:someprotocol,anotherprotocol {

The protocol implementation is written here

}

A detailed description of how to add protocol consistency in this way can be referenced by extending protocol consistency.

Attention:

If you add new features to an existing type by extension, the new feature is available for all existing instances of that type, even if they were created before the extension definition.

Computed properties

An extension can add a computed instance property and a computed type property for an existing type. The following example adds five computed instance properties to Swift's built-in Double type, providing basic support for collaborating with distance units:

Extension Double {

var km:double {return self * 1_000,0}

var m:double {return self}

var cm:double {return self/100.0}

var mm:double {return self/1_000.0}

var ft:double {return self/3.28084}

}

Let Oneinch = 25.4.mm

Print ("One inckh is \ (oneinch) meters")

Let Threefeet = 3.ft

Print ("Three feet is \ (threefeet) meters")

The meaning of these computed attribute representations is to treat a Double value as a length value under a unit. Even though they are implemented as computed properties, the names of these properties can still be immediately followed by a floating-point literal, which is used by point syntax to achieve distance conversions.

  

    

  

Swift Learning: extension (Extensions)

Related Article

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.