Tuesday, June 4, 2019

Mix Points


  • Object-oriented concepts don’t work well with structs and enums: a struct cannot inherit from another struct, neither can an enum inherit from another enum. So inheritance - one of the fundamental object-oriented concepts - cannot be applied to value types. On the other hand, value types can inherit from protocols, even multiple protocols. Thus, with Protocol Oriented Programming, value types have become first-class citizens in Swift.
  • Apple tells us: “Don’t start with a class, start with a protocol.”
  • Swift, just like many other programming languages, does not support multiple inheritances.
  • In Swift, you can extend a protocol and provide a default implementation for methods, computed properties, subscripts, and convenience initializers.
  • Swift does not allow multiple inheritances for classes. However, Swift types can adopt multiple protocols.
  • Objective-C does not support multiple inheritances, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.
  • Swift 5: Enumerations can have methods associated with them.
  • One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.
  • Classes, enumerations, and structs can all adopt protocols.
  • You can make generic forms of functions and methods, as well as classes, enumerations, and structures.