Monthly Archives: April 2022

Swift Protocols and Generics, Part 1: Protocol 和其他 Type 有什麼不一樣?

這是一系列以「建構基礎概念」為目標,希望能幫助 Swift 開發者更加瞭解 protocols 和 generics 的文章。文章索引、相關資源以及較詳細的介紹,請見系列簡介

希望讀者在看到文章裡提出問題的時候(請注意 [Q] 標示),能夠一起試著解釋看看,好深化你個人的理解。

Essential Questions

如果有人問你這個問題,你會怎麼解釋?[Q]

核心問題:

Protocol 作為一個 type,和 enum, struct, class… 等等其他的 type 有什麼不一樣?

延伸問題:

在 Swift 5.6 以後,protocol type 的 variables 前面要加上 any 一字。為什麼要改成這樣?

Continue reading