それは無理ですSwiftでは、associatedtypeがあるprotocolにはキャストできません。 こんにちは。FiNCのiOSチームで開発をしているtakasekといいます。Swiftの型の話をします。 どういうことSwiftのprotocolは、associatedtypeがない場合に限り、存在型(そのprotocolと同じメンバを持つ具体型)として扱うことができます。(追記: 正確には「associatedtypeがない、かつ var hoge: SelfのようなSelfを使うメンバもない場合に限り」です。ご指摘感謝) しかしprotocolがassociatedtypeを持つ場合、型の構造が静的に定まりません。型情報を抽象的に扱う必要があるので、統一的に処理する場合genericなfuncを通さなくてはなりません。 protocol Animal { associatedty
![[Swift] associatedtypeのあるprotocolにキャストする – FiNC Tech Blog – Medium](https://cdn-ak-scissors.b.st-hatena.com/image/square/d1e8e318935200f33a27b6b2ab83ac8da85fd6a5/height=288;version=1;width=512/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1200%2F1%2AwxTdsz74_53nXzhYv9X_Vw.png)