The Protocol in Swift (protocol) Learning Tutorial

Source: Internet
Author: User
First, Introduction

The Protocol deals with properties and methods that act like abstract classes in Java, and types in swift implement some of the agreed properties and methods by adhering to the protocol. The protocol in Swift is declared using the Protocol keyword. The protocol in Swift also has a very interesting feature, and the protocol can be extended to implement some methods and additional functionality.

Ii. defining properties and methods in the agreement

The properties defined in the agreement are only contract names and types, and in the implementation of a specific type it can be either a storage property or a computed property, and the protocol also needs to specify whether the property is readable or readable writable. The sample code is as follows:

Protocol Myportocol {  //Definition Instance Properties  //Readable  var name:string{get}  //readable writable  var age:int{set get}  // Readable  var nameandage:string{get}  static var classname:string{get}}class myclass:myportocol {  var name: String  var age:int  var nameandage:string{    get{      return "\ (name)" + "\ (age)"    }  }  static var classname:string{    get{      return "MyClass"    }  }  init () {    name = "HS" Age    = }  }

It is important to note that the readability of the protocol is not read-only, the property contract in the protocol is readable and writable, and when implemented, the property must be readable and writable, but if the agreement is made readable, this property can be read-only or readable and writable, depending on the implementation.

The method that is agreed in the protocol can be either an instance method or a type method, as in the following example:

Protocol Myportocol {  func logName ()  static func logclassname ()}class myclass:myportocol {  var name:string  var age:int  init () {    name = "HS" Age    = +  }  func logName () {    print (name)  }  static Func Logclassname () {    print (className)  }}

Similarly, the construction method can be defined in the protocol.

Iii. Characteristics of the agreement

Although there is no implementation of any properties and methods in the protocol, it can still be used as a type and is widely used in function parameters and return values, as shown in the following example:

Protocol Myportocol {  //define instance Properties  var name:string{get}  var age:int{set get}  var nameandage:string{ Get}  static var classname:string{get}  func logName ()  static func logclassname ()}//the protocol type as parameter Func test ( Param:myportocol) {  param.logname ()}

Protocol as a type of this usage another application point is in the collection type, and the protocol can act as a collection type for all compliance with this Protocol.

The protocol can inherit like other types, and the sub-protocol automatically owns the properties and methods of the parent Agreement Convention. The protocol can also be defined by the class keyword, with the following examples:

Protocol Myportocol {  //define instance Properties  var name:string{get}  var age:int{set get}  var nameandage:string{get}  static var classname:string{get}  func logName ()  static func logclassname ()}//only classes can inherit this Protocol protocol Mysubportocol:class,myportocol {  }

Since the protocol can be used in the same way as other types, it can also be checked and converted using is,as?,as!, and more usage of is,as can be seen with regard to the type conversion of Swift.

The protocol can also define the properties or methods in which it is optional, that a class that adheres to this protocol may or may not implement optional properties and methods, however, the declaration as optional requires this protocol to be of type @objc, as in the example below:

@objc protocol Myportocol {  //define instance Properties  var name:string{get}  var age:int{set get}  var nameandage: String{get}  static var classname:string{get}  func logName ()  //optional implementation  optional static Func Logclassname ()}

The protocol in Swift also has a very important feature that can be extended to implement properties, methods, and subscripts. This is handy for the methods of some general-purpose classes, which are equivalent to the default implementation of all classes that inherit this protocol, such as the following:

Protocol Myportocol {  //define instance Properties  var name:string{get}  var age:int{set get}  var nameandage:string{get}  static var classname:string{get}  func logName ()  static func logclassname ()}extension myportocol{  var name:string{    return "HS"  }}
  • 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.