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