Generic Protocol in Swift

Tram Ho

In Swift, there are two ways to create generic protocols:

  1. Add Self Requirement.
  2. Use Associated Types

1. Protocol Self Requirement

The type of parameters is of type Self . When a class, struct or an enviroment adopt this protocol, the type will automatically switch back to the type of that adopt protocol object.

The protocol with self will not work in the following cases:

  • Giá trị của kiểu Self không được phép (The property does not match the type Self ).
  • If Self returns a type of a function. For example:

What do you think I write the type of parametes as the protocol type? When writing parametes as a protocol type, it is called Without Self Requirement :

I will explain to you the advantages of using self requiment compared to Without Self Requirement :

Here is a table comparing the advantages of with self requirement.

1.1 Think heterogeneous? Think homogeneous?

You just need to understand it simply:

– heterogeneous : Elements in an array can have different types: [1, 6, 4, “2”]

Homogeneous : In contrast to heterogeneous, elements in an array must be of the same type.

Functional, functional programming for collections in Swift
Generic Protocol in Swift

Swift stepped into the top 10, Java still held the throne. “]]

I will make an example of With Self Requirement featuring homogeneous:

  • With Self Requirement :

Despite the student objects, techter is of type Human but cannot be saved in a Collection.

  • Without Self Requirement :

1.2 Dynamic Dispatch? Static Dispatch?

Dynamic Dispatch : determine the function to run during runtime.

Static Dispatch : determines the function to be run during compilation.

A function that is not declared in the protocol but declared in extension is static dispatch. (You can see examples online to understand the problem) .

To understand about dynamic dispatch with statich dispatch in protocol you can read .

I will take the static dispatch Self Requirement Protocol and Without Self Requirement with dynamic dispatch

2. Protocol Associated Types

When using Self, only the type of protocol that the protocol can use. If you use Protocol Associated Types, you can declare one or more associatedtypes in a protocol.

Protocol Associated Types (PAT) = Type Alias ​​+ Generics

The following example will show you how to use Associated Types in the protocol:

Any class, struct, enum that dopt protcol GenericProtocol must implement anyProperty . However, the type of anyProperty has not been explicitly declared. Therefore, in class, struct or enum, there must be clear definition or implicit definition.

2.1 Define Associated Type Implicitly

You can specify the type of myType based on the value associated with anyProperty

Now, myType has been defined as String , but you can do the following:

2.2 Define Associated Type Explicitly

You can define associated type by calling typealias below or you can define myType as I wrote above.

or you can define myType into any type you want:

The article is still incomplete, all comments you can comment below, so that the article is better.

References:

Swift and Kotlin entered a recession!
Select Objective-C or Swift for the programming project
Share the news now

Source : viblo .u8e87b6b0cd9cf98eb52426640d8607fb { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u8e87b6b0cd9cf98eb52426640d8607fb:active, .u8e87b6b0cd9cf98eb52426640d8607fb:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u8e87b6b0cd9cf98eb52426640d8607fb { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u8e87b6b0cd9cf98eb52426640d8607fb .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u8e87b6b0cd9cf98eb52426640d8607fb .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u8e87b6b0cd9cf98eb52426640d8607fb:hover .postTitle { text-decoration: underline!important; } Swift và Kotlin bước vào thời kỳ thoái trào! .u8db785ccac4c9cd5f06d56993404c167 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u8db785ccac4c9cd5f06d56993404c167:active, .u8db785ccac4c9cd5f06d56993404c167:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u8db785ccac4c9cd5f06d56993404c167 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u8db785ccac4c9cd5f06d56993404c167 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u8db785ccac4c9cd5f06d56993404c167 .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u8db785ccac4c9cd5f06d56993404c167:hover .postTitle { text-decoration: underline!important; } Lựa chọn Objective-C hay Swift cho dự án lập trình