๐ดProtocol Extensions
provide default implementations. (dynamic method dispatch) ๐ ๐ ๅ่
add new functionalities: (static method dispatch)
add methods
add (convenience) initializers (but not convenience override init)
add subscripts
add computed properties
make existing types conform to protocol.
ๅ๏ผprotocol extension ่ฃก้ขๅฏไปฅๅ nested types ๅ๏ผ
็ญ๏ผ็ฎๅ็ไพไธ่กโ๏ธ (๐ ๐ฃ ่จ่ซ)
Extensions can add new nested types to existing class, struct, and enum. (but not protocol extensionsโ) ๐Nested Types in Extensions
Protocol Definition vs. Protocol Extension
Declaring a method inside the protocol (definition) creates an extension point โ a method that we encourage conforming types to override.
If we put methods into the protocol extension but not the protocol definition, all conforming types still get the method, except now weโre making it harder for them to override. (๐ไพไบ)
define requirements for conforming types. (๐ไพไธ)
Conditional Protocol Extensions
Last updated