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