Extensions by protocol conformance

Another effective technique is to organize your extensions based on protocol conformance. You’ve already seen this technique used in Chapter 17, “Protocols”. As an example, let’s make CheckingAccount conform to CustomStringConvertible by adding the following extension:

extension CheckingAccount: CustomStringConvertible { public var description: String {

return "Checking Balance: $(balance)"

}

}

This extension implements CustomStringConvertible, and more importantly:

  • Makes it obvious description is part of CustomStringConvertible
  • Doesn’t help conform to other protocols
  • Can easily be removed without doing collateral damage to the rest of

CheckingAccount

  • Is easy to grok!

results matching ""

    No results matching ""