Extensions and Protocols

Source: Internet
Author: User

1. Extension: Swift uses an extension mechanism that can be used in the original class struct and enumeration, in the following format:

Extension type name {

New features

}

New features can be added in the original type (class, struct, enum) including: 1, instance computed properties and static computed properties 2, instance method and static method 3, constructor 4, subscript.

2, protocol: All methods are abstract, called Protocol (Protocol) similar to the C # interface. The protocol is highly abstract, and he has only abstract method names, parameter lists, and return values. Implementations become compliance protocols that implement both interfaces.

Declaration Agreement:

Protocol protocol Name {

Protocol contents such as Func GetInfo () do not need to implement curly braces []

}

Realize:

Type type name: Parent class (optional), Protocol 1, Protocol 2{

Concrete implementation of the Func GetInfo () {//Method implementation}

}

Note Static protocol, before adding class or Static, class representing classes (class Func GetName ()), static struct, enum enum enumimp:account{static func GetName () {}}

Extensions and Protocols

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.