ios-swift-Agreement and expansion

Source: Internet
Author: User

1 Protocol (protocol)

Use the keyword protocol to create the agreement.

    protocol Exampleprotocol {        get  }        mutating func adjust ()    }

Classes, enumerations, and structs all support protocols.

Lass Simpleclass:exampleprotocol {var simpledescription:string="A very simple class."var anotherproperty:int=69105func Adjust () {simpledescription+="Now 100% adjusted."}}var a=Simpleclass () a.adjust ( ) Let Adescription=a.simpledescriptionstructSimplestructure:exampleprotocol {var simpledescription:string="A Simple Structure"mutating func adjust () {simpledescription+="(adjusted)"}}var b=simplestructure () b.adjust ( ) Let bdescription= B.simpledescription

Note the keywords mutating that are used in struct simplestructure to implement the methods in the mutating protocol. In the class, Simpleclassdoes not require the keyword mutating implement the Protocol method, because the method in the class itself does not require the keyword mutating declaration.

2 Expansion (extension)

Use keyword extension to create an extension of an existing type.

    extension Int:exampleprotocol {        var simpledescription:string {            return]  The number \ (self)"        }        mutating func adjust () {            $        }    }    Print (7. simpledescription)

Well, try.

ios-swift-Agreement and expansion

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.