Module Outline

Use the following resources and work with your mentor to master the objectives for this module. Practice by completing the Mini-Project, Project, and Challenges.

Objectives

Lesson Video

Preclass Videos

Video Challenges

  • Plan MVC Architecture for a Family Recipes App

    Draw the MVC triangle. Take 5 minutes to think through a simple Family Recipe App listing favorite family recipes. What type of model objects are there? What types of views are there? What View Controllers are there? What model controllers would be helpful? Add each of them to a list. Discuss with your neighbor.

  • Cars

    Create a Playground and define a Car class with properties for make, model, year, vin. Create a memberwise initializer. Initialize three different objects.

  • Bookshelf

    Do the following in a new Playground.

    • Create a Book object with title, author, and genre properties, and a memberwise initializer.
    • Create a Bookshelf object that will serve as a model object controller. The Bookshelf should have a books array and addBook(book: Book) and removeBook(book: Book) functions that manage the array.
    • Create a couple of books and add/remove them from the array in the Playground.
  • Create Your Own Singleton

    Give the students 5-10 minutes to build a model and controller for an application for a Parking Structure.

    Car.swift -> make, model, year, etc CarController.swift -> sharedController, parkedCars, addCar, removeCar

    We are not doing any persistence yet. They should have a simple model object and a controller that holds an array of the objects.

Stretch Problem

    Equatable

    Start with the following code:

    class Person {
      let firstName: String
      let lastName: String
      let age: Int
    
      init(firstName: String, lastName: String, age: Int) {
        self.firstName = firstName
        self.lastName = lastName
        self.age = age
      }
    }
    
    let james = Person(firstName: "James", lastName: "Pacheco", age: 26)
    let andrea = Person(firstName: "Andrea", lastName: "Mower", age: 24)
    let carol = Person(firstName: "Rebecca", lastName: "Mordo", age: 30)
    
    let clubMembers: [Person] = [james, andrea, carol]
    
    1. Initialize two more Person objects. Add one of them to the club array, but do not add the other.
    2. Now, create a function called areYouAMemberOfTheClub that takes a Person object as a parameter and returns a Bool indicating whether or not the person passed in is a member of the club. It should return true for the person that you added to the array of club members and false for the other person.
      • note: there are a couple of ways to do this, but in all ways you will be using the clubMembers array as a reference.
    3. Perhaps you thought the best way to accomplish the above task was to loop through the clubMembers array in your function and see if the person passed in is equal to any of them using person == clubMembers[i], or something along those lines. Notice that you cannot use the == operator. This is because you must conform to the Equatable protocol. Go look it up in documentation.
    4. Make the Person object conform to the Equatable protocol and outside of the Person class create the necessary Equatable function. You should now be able to use the == operator between two Person objects, making it easier to make your areYouAMemberOfTheClub function. By conforming to the Equatable protocol, you are also given some array methods that help solve this problem (you don't have to use them, but they make your life easier).

    ♦ Black Diamond

    Create a new X-Code Project called ClubMembers. Copy over your Person class, clubMembers array, and make a new array that contains all of your Person objects, whether club members or not. Make sure to copy over your Equatable function as well (all of these should be put into the proper files, which you'll need to make). Then create a tableView that displays all people with a subtitle that says club member or not a club member. Add a button that will reload your tableView and only show club members.

  • Hint
  • Solution

Guided Project

    Playlist

    Playlist teaches proper MVC design through creating Playlist and Song objects with corresponding controllers, and teaches the singleton pattern.

Project

  • Journal

    Students will build a simple Journal app to practice MVC separation, protocols, master-detail interfaces, table views, and persistence.

    Journal is an excellent app to practice basic Cocoa Touch princples and design patterns. Students are encouraged to repeat building Journal regularly until the principles and patterns are internalized and the student can build Journal without a guide.

    ReadMe

Mastery Review

Module 8 - MVC, Model Objects, Model Object Controllers
  • Expand all
  • Collapse all
  • Equatable stretch problem
  • Challenges
    • Plan MVC Architecture for a Family Recipes App objective challenge
    • Cars objective challenge
    • Bookshelf objective challenge
    • Create Your Own Singleton objective challenge
  • Playlist guided project