Generic Protocol trong Swift

Tram Ho

Trong Swift có 2 cách để tạo protocol generic:

  1. Thêm Self Requirement.
  2. Dùng Associated Types

1. Protocol Self Requirement

Kiểu của các parameters là kiểu Self. Khi class , struct hoặc enum adopt protocol này, thì kiểu sẽ tự động chuyển về kiểu của object adopt protocol đó.

Protocol with self sẽ không làm việc trong các case sau:

  • Properties of type Self aren’t allowed. (Thuộc tính không phù hợp với type Self).
  • Nếu Self trả về là một kiểu của của một function. Ví dụ:

Các bạn lại nghĩ tôi viết kiểu của parametes là kiểu protocol thì sao. Khi mà viết kiểu parametes là kiểu protocol thì nó được gọi là Without Self Requirement:

Mình sẻ giải thích cho các bạn ưu điểm khi sử dụng self requiment so với Without Self Requirement : 

Đây là bảng so sánh ưu điểm của with self requirement.

1.1 Think heterogeneous? Think homogeneous?

Chỉ cần các bạn hiểu đơn giản là:

– heterogeneous: Các phần tử trong một mảng có thể có các kiểu khác nhau VD: [1, 6, 4, “2”]

– homogeneous: Ngược lại với heterogeneous là các phần tử trong một mảng phải cùng kiểu.

.u44423d961e462a267c5eefad6d16f7ed { 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; } .u44423d961e462a267c5eefad6d16f7ed:active, .u44423d961e462a267c5eefad6d16f7ed:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u44423d961e462a267c5eefad6d16f7ed { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u44423d961e462a267c5eefad6d16f7ed .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u44423d961e462a267c5eefad6d16f7ed .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u44423d961e462a267c5eefad6d16f7ed:hover .postTitle { text-decoration: underline!important; }

  Hiệu năng, lập trình hàm cho collections trong Swift

.u9b74d707133658e1ce96e1b6e703899f { 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; } .u9b74d707133658e1ce96e1b6e703899f:active, .u9b74d707133658e1ce96e1b6e703899f:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u9b74d707133658e1ce96e1b6e703899f { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u9b74d707133658e1ce96e1b6e703899f .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u9b74d707133658e1ce96e1b6e703899f .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u9b74d707133658e1ce96e1b6e703899f:hover .postTitle { text-decoration: underline!important; }

  Generic Protocol trong Swift

Swift chen chân vào top 10, Java vẫn giữ vững ngôi vương””]

Mình sẽ làm ví dụ về With Self Requirement có đặc điểm là homogeneous:

  • With Self Requirement :

Mặc dù các object student, techter đều có kiểu Human nhưng không thể lưu vào trong một Collection.

  • Without Self Requirement:

1.2 Dynamic Dispatch ? Static Dispatch ?

Dynamic Dispatch: xác định hàm được chạy trong quá trình runtime.

Static Dispatch: xác định hàm được chạy trong quá trình biên dịch.

Một hàm không được khai báo ở protocol mà được khai báo ở extension thì nó là static dispatch. (Các bạn có thể xem các ví dụ trên mạng nhé để hiểu vấn đề).

Để hiểu về dynamic dispatch với statich dispatch trong protocol các bạn có thể đọc .

Mình sẽ lấy ví dụ về Self Requirement Protocol có tính static dispatch và Without Self Requirement có tính dynamic dispatch

2. Protocol Associated Types

Khi mà dùng Self chỉ có thể dùng được kiểu mà protocol đó adopt. Nếu dùng Protocol Associated Types có thể khai báo một hoặc nhiều associatedtypes trong một protocol.

Protocol Associated Types (PAT)= Type Alias + Generics

Ví dụ dưới đây sẽ cho các bạn thấy cách sử dụng Associated Types trong protocol:

Bất kỳ class, struct, enum mà dopt protcol GenericProtocol đều phải implement anyProperty. Tuy nhiên , kiểu của anyProperty chưa được khai báo rõ ràng.Vì vậy trong class, struct hoặc enum phải định nghĩa rõ ràng hoặc định nghĩa ngầm.

2.1 Define Associated Type Implicitly

Bạn có thể xác định kiểu của myType dựa vào giá trị liên kết với anyProperty

Bây giờ , myType đã được định nghĩa là String, tuy nhiên bạn có thể làm như bên dưới:

2.2 Define Associated Type Explicitly

Bạn có thể định nghĩa associated type bằng cách gọi typealias như bên dưới hoặc có thể định nghĩa myType như cách tôi viết ở trên.

hoặc bạn có thể định nghĩa myType thành một kiểu bất kỳ mà bạn muốn:

Bài viết đang còn chưa đầy đủ, mọi ý kiến đóng góp các bạn có thể comment xuống bên dưới, để bài viết được tốt hơn.

Tài liệu tham khảo:

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

Chia sẻ bài viết ngay

Nguồn bài viết : 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