1

I am a iOS/swift newbie coming from the C# world. In C#, I've followed the principle of 'programming to an interface' whenever possible. Is that a pattern that people follow in the swift world? For instance if there is a CustomerService, do I start with a CustomerServiceProtocol that the CustomerService conforms to?

Cœur
  • 37,241
  • 25
  • 195
  • 267
darthjit
  • 1,555
  • 13
  • 20
  • I don't really follow the _programming against interfaces_, why you mean? – Victor Sigler Sep 02 '15 at 21:57
  • As a simple answer, yes I have found that using protocols instead of explicit classes makes things a lot cleaner in Swift, and I am very much looking forward to being able to use protocol extensions in Swift 2. – Rupert Sep 02 '15 at 23:46
  • @VictorSigler I meant "program to an interface" http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface – darthjit Sep 02 '15 at 23:56

1 Answers1

4

I'm sure you are well aware of the principle that states, "prefer composition to inheritance." Protocols, and now especially with Swift 2's protocol extensions, are a great tool to create functionality via composition. The following blog post offers a great explanation and comparison to C#.

https://www.andrewcbancroft.com/2015/08/06/analyzing-swift-protocol-extensions-and-c-abstract-classes/

Mr Beardsley
  • 3,743
  • 22
  • 28