In the case of a class created from scratch, extensions are a powerful type of documentation through structure. You put the core of your class in the initial definition, and then add extensions to provide additional functions. For example, adding protocol attachment. It provides the locality of the contained code:
struct Foo { let age: Int } extension Foo: CustomStringConvertible { var description:String { return "age: \(age)" } }
Can I put a protocol and computed property in a structure declaration? Absolutely, but when you have more than one or two properties, it starts to become dirty and difficult to read. It is easier to create errors if the code is not clean and readable. Using extensions is a great way to deal with difficulties that arise with complexity.
ColGraff Mar 28 '16 at 14:19 2016-03-28 14:19
source share