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 ...

Get Swift 3 ObjectOriented Programming - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.