Adding an object-oriented data repository to a project

Now, we will add one protocol and many classes we created in the previous chapter to generate the GameRepository class. We want to display a list of game names in the Picker View. We will add the following Swift source files in the project within the Chapter8 group:

  • Identifiable.swift
  • Entity.swift
  • Repository.swift
  • Game.swift
  • GameRepository.swift

Click on the Chapter8 group in Project Navigator (the icon represents a folder). Do not confuse it with the Chapter8 project that is the parent for the Chapter8 group. Navigate to File | New | File... and select Swift File as the template for your new file. Then, click on Next and enter Identifiable in the Save As textbox. Make sure that Chapter8 with the folder icon is selected in the Group drop-down menu, as shown in the next screenshot, and then click on Create. Swift will add the new Identifiable.swift source file to the Chapter8 group within the Chapter8 project:

Adding an object-oriented data repository to a project

Add the following code for the recently created Identifiable.swift source file. The code file for the sample is included in the swift_3_oop_chapter_08_09 folder.

    public protocol Identifiable { 
      var id: Int { get } 
    } 

Follow the previously explained steps to add a new Entity.swift source file to the Chapter8 group within the Chapter8 project. Add the following code to the new source file. The code file for the sample is included in the swift_3_oop_chapter_08_09 folder.

    open class Entity: Identifiable { 
      open let id: Int 
 
      init(id: Int) { 
        self.id = id 
      } 
    } 

Follow the previously explained steps to add a new Repository.swift source file to the Chapter8 group within the Chapter8 project. Add the following code to the new source file. The code file for the sample is included in the swift_3_oop_chapter_08_09 folder.

    open class Repository<Element: Identifiable> { 
      open func getAll() -> [Element] { 
        return [Element]() 
      } 
    } 

Follow the previously explained steps to add a new Game.swift source file to the Chapter8 group within the Chapter8 project. Add the following code to the new source file. The code file for the sample is included in the swift_3_oop_chapter_08_09 folder.

    open class Game: Entity, CustomStringConvertible {
      open var name: String
      open var highestScore: Int
      open var playedCount: Int
      open var description: String {
        get {
          return "id: (id), name: "(name)", highestScore: (highestScore), 
          playedCount: (playedCount)"
        }
       }
       init(id: Int, name: String, highestScore: Int, playedCount: Int) {
         self.name = name
         self.highestScore = highestScore
         self.playedCount = playedCount
         super.init(id: id)
       }
     }

Follow the previously explained steps to add a new GameRepository.swift source file to the Chapter8 group within the Chapter8 project. Add the following code to the new source file. The code file for the sample is included in the swift_3_oop_chapter_08_09 folder.

    open class GameRepository: Repository<Game> {
      open override func getAll() -> [Game] {
        var gamesList = [Game]()
        gamesList.append(Game(id: 1, name: "Invaders 2017", highestScore: 1050, 
        playedCount: 3_050))
        gamesList.append(Game(id: 2, name: "Minecraft", highestScore: 3741050, 
        playedCount: 780_009_992))
        gamesList.append(Game(id: 3, name: "Minecraft Story Mode", 
        highestScore: 67881050, playedCount: 304_506_506))
        gamesList.append(Game(id: 4, name: "Soccer Warriors", highestScore: 10_025, 
        playedCount: 320_450))
        gamesList.append(Game(id: 5, name: "The Walking Dead Stories", 
        highestScore: 1_450_708, playedCount: 75_405_350))
        gamesList.append(Game(id: 6, name: "Once Upon a Time in Wonderland", 
        highestScore: 1_050_320, playedCount: 7_052))
        gamesList.append(Game(id: 7, name: "Cars Forever", highestScore: 6_705_203, 
        playedCount: 850_021))
        gamesList.append(Game(id: 8, name: "Jake & Peter Pan", highestScore: 4_023_134, 
        playedCount: 350_230))
        gamesList.append(Game(id: 9, name: "Kong Strikes Back", 
        highestScore: 1_050_230, playedCount: 450_050))
        gamesList.append(Game(id: 10, name: "Mario Kart 2017", 
        highestScore: 10_572_340, playedCount: 3_760_879))
        return gamesList
      }
    }

We added all the necessary source files to include the protocol and the classes that allow us to work with the GameRepository class in our app. The following screenshot shows the Project Navigator with all the new files added to the Chapter8 group. In this case, we will add all the files to the same group. However, in more complex apps, it would be convenient to split the files in different groups to have a better organization of the code:

Adding an object-oriented data repository to a project

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.98.186