Complementing @Thomas Schar's answer. The magic of the Swift protocol comes from the extension.
- Swift protocols can receive implementations through the extension (Swift | 2). The Java 8 interface may have standard implementations, but it cannot be done retroactively.
- In fast mode, you can "retroactively" add protocol requirements (and its implementation if necessary) for any class or structure.
- Swift protocols do not match the generic type configuration template (ie <...>), but the type scheme (for example, related types). It can be confusing at startup, but can be avoided in some cases - “blindness of the angle bracket”.
- Swift has an extended type pattern mapping, which allows a specific question about where and how the requirements and protocol extensions apply. It can be confusing when it comes to Java, but it has a lot of power.
- The Swift protocol can be configured for the / param property (i.e. celebrator: Protocol)
One thing that made me scratch my head for several hours was that not all protocols can be used as a type of property. For example, if you have a protocol with typealias, you cannot directly use it as a type of property (this makes sense if you think about it, but from Java, we really want to have a property like userDao: IDao).
Jeremy Chone Jun 29 '15 at 17:35 2015-06-29 17:35
source share