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
- You will be able to describe, identify, and implement proper Model-View-Controller principles
- You will be able to create a custom class for storing model data
- You will be able to store data on and retrieve data from a model controller object
- You will be able to identify, describe, and implement the singleton pattern
Model-View-Controller is an essential design pattern to follow when developing for iOS. We will talk about following MVC principles as we architect and plan our our applications.
This lesson provides many examples for thinking through MVC architecture in a variety of contexts. Students should not get hung up on all of the different examples, but should focus on the MVC concepts.
Required Preclass Resources
-
Stanford iOS Development MVC Description
Watch from 13:30 - 29:55.
- Apple's MVC Documentation
- Understanding Model-View-Controller
- Cocoa Core Competencies: Model Objects
- Cocoa Core Competencies - Controller Objects
- Create, Read, Update, and Delete
- Cocoa Core Competencies - Singleton
- Avoiding Singleton Abuse
- Singletons, AppDelegates, and Top-Level Data
Lesson Video
Preclass Videos
-
iOS Cohort - Andrew Madsen
Overview of Model View Controller
-
iOS Cohort - Andrew Madsen
Creating Model Object Classes
-
iOS Cohort - Andrew Madsen
Model Controllers
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)
andremoveBook(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
- Initialize two more
Person
objects. Add one of them to the club array, but do not add the other. - Now, create a function called
areYouAMemberOfTheClub
that takes aPerson
object as a parameter and returns aBool
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.
- note: there are a couple of ways to do this, but in all ways you will be using the
- 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 usingperson == 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. - Make the
Person
object conform to the Equatable protocol and outside of thePerson
class create the necessary Equatable function. You should now be able to use the==
operator between twoPerson
objects, making it easier to make yourareYouAMemberOfTheClub
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). - Hint
- Solution
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]
♦ 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.
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
- 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