The protocol in Swift

Source: Internet
Author: User

Protocol :

The 1.Swift protocol is used to define the specifications that multiple types should follow

2. The agreement defines a specification that does not provide any implementation

3. The agreement unifies the attribute name , method, subscript , but the protocol does not provide any implementation

4. syntax format :

[ modifier ] Protocol protocol name : Parent Protocol 1, Parent agreement 2, ... {// protocol content }

Modifier : can be omitted , can also be private, internal, public one

The protocol name should have the same naming convention as the class name

A protocol can have multiple direct parent protocols , but the protocol only inherits the protocol and cannot inherit the class

Content of the Protocol : Specify the functions that the Protocol's implementation must provide, such as attributes , methods , constructors and subscripts, etc.

syntax to implement the protocol :

1.Struct struct name : First Protocol , Second protocol , ... {// implement protocol requirements }

2.Class class name : Superclass, First protocol, Second protocol , ... {// requirements for agreement }

protocol Specifies attribute requirements

1. syntax for defining attribute requirements in the protocol :

Class Var property name : type {get set}

Description : Class is optional , if there is a class keyword, the description is a type attribute , otherwise the instance property

You cannot replace class with Static

Get and set parts : just write get, set , No implementation required , set optional

Here is the Code section:

Viewcontroller.swift

Import Uikitclass Viewcontroller:uiviewcontroller, buttonviewdelegate {        //the properties specified in the agreement    var testvalueone:string = " Testvalueone "    var testvaluetwo:string =" Testvaluetwo "        var buttonview:buttonview!    Override Func Viewdidload ()    {        super.viewdidload ()                Self.buttonview = Buttonview.buttonview ()        Self.buttonView.frame = CGRectMake (0, 0, $, $)        Self.buttonView.center = Self.view.center        Self.buttonView.delegate = self        self.view.addSubview (self.buttonview)    }        //Mark-buttonviewdelegate        func Buttonview (Buttonview:buttonview, Didclickbutton Button:uibutton)    {        print ("Click the button inside the Buttonview")}    }

  

Buttonview.swift (This class was created by Xib)

Import Uikitprotocol testdelegateone{    //define protocol properties    Var testvalueone:string {Get Set}}protocol testdelegatetwo{< c2/>//define the Protocol attribute    var testvaluetwo:string {Get Set}}//protocol can implement multiple inheritance protocol Buttonviewdelegate:testdelegateone, Testdele gatetwo{    //Protocol definition Method    func buttonview (Buttonview:buttonview, Didclickbutton Button:uibutton)}class Buttonview : uiview{    //define proxy object    var delegate:buttonviewdelegate?        Quick Create object Method    class Func Buttonview ()-Buttonview    {let        Buttonview:buttonview = Nsbundle.mainbundle () . loadnibnamed ("Buttonview", Owner:nil, Options:nil). First as! Buttonview        return Buttonview    }        @IBAction func buttonclick (Sender:uibutton)    {        if let Delegate = self.delegate {            Delegate.buttonview (self, Didclickbutton:sender)}}    

The protocol in Swift

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.