Create a Model and Mock Instances
Build an App Like Lego, with SwiftUI — Tutorial 11
1. Introduction
Broadly speaking, an app has two facets: model and view. A view is something you see or touch or otherwise interact with directly as a human. A model is something behind the scenes that stores data or calculates results.
A view’s job is to show visual layout, color, text, images, buttons, tab bar, and other controls on the device’s screen. A model contains the raw words, numbers, names of images, dates, any calculations, and business logic.
A model is like the bones and brain. A view is like the skin, eyes, and ears.
In this tutorial, we will build a model and mock data.
So far in our app, we have created only views, like NewsCell. At the end of the previous Tutorial 10 we refactored the NewsCell to expose properties that we can customize to create different NewsCell instances.
In this Tutorial 11, we will add a model called Article to contain the raw news article information. In the next tutorial, we will display those articles in news cells.
We’ll pick up here where the last tutorial left off. Ideally, you have completed the previous tutorials in this series. Or, you can download the prepared project, ready to start this tutorial.
2. Model Types
Swift stores words and names in a type called a String. A String is a model, which stores a “string” of characters, such as letters, numbers, spaces, and punctuation, to make a word, sentence, paragraph or longer. In contrast to a Text view, a String only stores the “string” of characters, not the visual characteristics like font, color, and size.
Swift stores dates and times in the Date type.
Numbers are usually stored as an Int if they are integers (whole numbers like 0, 1, 234, -78) or as Double if they can have a fractional component (like 0.78, 1.23 or -45.6).
3. Create the Model Code
Each news article will consist of a title, date, detail, and the name of two images. Let’s create an Article type to store this information for each article.
👉 In Xcode, use the File menu’s New File… command to create a new file called Article.swift, using the Swift File template.


👉 Type or paste the code below. It’s good practice to type in the code to familiarise yourself with Xcode’s editing and autocompletion features.








