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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.