Creating a data repository with generics and protocols
Now, we want to create a repository that provides us with entities so that we can apply the functional programming features included in Swift to retrieve and process data from these entities. First, we will create an Identifiable
protocol that defines the requirements for an identifiable entity. We want any class that conforms to this protocol to have a read-only id
property of the Int
type to provide a unique identifier for the entity. The code file for the sample is included in the swift_3_oop_chapter_07_11
folder:
import Foundation public protocol Identifiable { var id: Int { get } }
The next lines create a Repository<Element>
generic class, which specifies that Element
must conform to the recently created Identifiable
protocol in the generic type constraint. The class declares a getAll
method that we will override in the subclasses. The code file for the sample is included in the swift_3_oop_chapter_07_11...