O'Reilly logo

Swift 3 ObjectOriented Programming - Second Edition by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required