| Start |
|---|
| Leveraging Protocol Oriented Programming for Unit Testing Protocol Oriented Programming is a useful tool in order to easily write better unit tests for our code. Let's say we want to test a UIViewController that relies on a ViewModel class. The needed steps on the production code are:
Then, on unit test:
|
| Using protocols as first class types Protocol oriented programing can be used as a core Swift design pattern. Different types are able to conform to the same protocol, value types can even conform to multiple protocols and even provide default method implementation. Initially protocols are defined that can represent commonly used properties and/or methods with either specific or generic types. A default implementation for the get method can be created, though if desired conforming types may override the implementation. A value type that conforms to the ItemData protocol, this value type is also able to conform to other protocols. Here the item struct is extended to conform to a display item. An example call site for using the static get method. Different use cases will require different implementations. The main idea here is to show conformance from varying types where the protocol is the main point of the focus in the design. In this example perhaps the API data is conditionally saved to a Core Data entity. Here the Core Data backed class can also conform to the DisplayItem protocol. RemarksFor more information on this topic, see the WWDC 2015 talk Protocol-Oriented Programming in Swift (opens new window). There is also a great written guide on the same: Introducing Protocol-Oriented Programming in Swift 2 (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference