Skip to Main Content
Learning Swift, 3rd Edition
book

Learning Swift, 3rd Edition

by Jonathon Manning, Paris Buttfield-Addison, Tim Nugent
March 2018
Beginner content levelBeginner
378 pages
8h 45m
English
O'Reilly Media, Inc.
Content preview from Learning Swift, 3rd Edition

Chapter 5. Building the Model Object

Although we don’t yet have any functionality in the app beyond what the template gave us, we will need some sort of model object and storage for those objects when we get around to using them. For this reason, we’ll be creating the model part of our app first. We’ll create two classes for this: our model will be called Selfie, and our manager SelfieStore. Making the model first gives us a platform on which to build up the rest of the app without having to stub out functionality when it needs the model to be working.

The Selfie Object

The first object we need to create will be a new class to represent an individual selfie in the application:

  1. Create a new Swift file by going to File → New File.

  2. In the iOS category, under the Source heading, select the Swift File option.

  3. Name the file SelfieStore.swift.

  4. Save it into the Selfiegram folder and make it target the Selfiegram target (see Figure 5-1).

    lsw3 0501
    Figure 5-1. The model file
  5. Near the top of the file, underneath the import line, import the UIKit.UIImage library:

    import UIKit.UIImage

    We are only importing the UIImage part of UIKit because the UIKit framework is quite large, and we only need this one small piece of it. This will give us access to the UIImage class, which will hold the image data we need for our selfies. In our case we will be using it to store JPGs, but the class itself ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning To Program With Swift

Learning To Program With Swift

T. Rogers

Publisher Resources

ISBN: 9781491987568Errata Page