Protocol tutorial in Swift

Source: Internet
Author: User
Some basic attributes that need to be instantiated can be defined in the protocol. here, let's take a look at the protocol (protocol) tutorial in Swift. For more information, see I. INTRODUCTION

The protocol defines some attributes and methods. Their functions are similar to abstract classes in Java. the types in Swift comply with the protocol to implement some agreed attributes and methods. Protocols in Swift are declared using the protocol keyword. The protocol in Swift also has a very interesting feature. the protocol can be extended to implement some methods and additional functions.

II. define attributes and methods in the protocol

The attribute defined in the protocol only specifies the name and type. in the implementation of a specific type, it can be either a Storage attribute or a computing attribute, you also need to specify whether the attribute is readable or writable in the protocol. The sample code is as follows:

Protocol MyPortocol {// defines the instance attributes // readable var name: String {get} // readable and 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 = 24 }}

Note that the protocol is not read-only, and the attribute conventions in the protocol are readable and writable. in implementation, this attribute must be readable and writable, but if the protocol is set to readable, this attribute can be read-only or writable, depending on the specific implementation.

The method agreed in the protocol can be an instance method or a type method, for example:

protocol MyPortocol {  func logName()  static func logClassName()}class MyClass: MyPortocol {  var name: String  var age: Int  init(){    name = "HS"    age = 24  }  func logName() {    print(name)  }  static func logClassName() {    print(className)  }}

Likewise, constructor methods can also be defined in the protocol.

III. protocol features

Although the protocol does not have any attribute or method implementation, it can still be used as a type and is widely used in function parameters and return values. The example is as follows:

Protocol MyPortocol {// defines the instance property var name: String {get} var age: Int {set get} var nameAndAge: String {get} static var className: string {get} func logName () static func logClassName ()} // use the protocol type as the parameter func test (param: MyPortocol) {param. logName ()}

Protocol as type this usage another application point is in the set type, the protocol can be used as all the set types that comply with this protocol.

Protocols can be inherited like other types. sub-protocols automatically have the attributes and methods agreed by the parent protocol. The protocol can also be defined by the class keyword that only the class can be followed. The example is as follows:

Protocol MyPortocol {// defines the instance property 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 MySubPortocol: class, MyPortocol {}

Since the protocol can be used like other types, of course it can also use is, ?, As! For more information about is and as, you can view Swift's content about type conversion.

The protocol can also define optional properties or methods. that is, classes that comply with this protocol can be implemented or optional properties and methods are not implemented. However, the declaration is optional and the protocol needs to be of the @ objc type. The example is as follows:

@ Objc protocol MyPortocol {// defines the instance property var name: String {get} var age: Int {set get} var nameAndAge: String {get} static var className: string {get} func logName () // optional static func logClassName ()}

The protocol in Swift also has a very important feature. it can be extended to implement attributes, methods, and underlying standards. This is very convenient for some methods of common classes, which is equivalent to all classes that inherit this protocol implement this method by default. The example is as follows:

Protocol MyPortocol {// defines the instance property 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 "}}

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.