Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Your First iOS & SwiftUI App: An App from Scratch
Jan 11 2022 Swift 5.5, iOS 15, Xcode 13
Part 2: SwiftUI Data
18. Create a Model

Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
About this episode
Create a basic data model for Bull’s Eye, that stores the target, current round and total score.
Instructors
Contributors
Instructor
Instructor
Illustrator
Video Editor
Over 300 content creators. Join our team.
Version history
iOS 15, Swift 5.5, Xcode 13 (Selected)
Jan 11 2022iOS 16, Swift 5.7, Xcode 14
Feb 13 2023iOS 14, Swift 5, Xcode 12
Dec 15 2020iOS 13, Swift 5, Xcode 11
Sep 3 2019iOS 12, Swift 4, Xcode 10
Jul 24 2018iOS 11, Swift 4, Xcode 9
Sep 19 2017iOS 10, Swift 3, Xcode 8
Nov 7 2016Swift 2
Sep 1 2015Leave a rating/review
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
This video Create a Model was last updated on Jan 11 2022
OK, let’s learn how we can put the theory of App Architecture into practice, by creating a simple data model for Bullseye.
To review, remember that we need three things: the target the user is aiming for, we need the current round, and we need the total score across all rounds. These will all be integer numbers.
OK - let’s open up Xcode and implement the Game struct.
Create group for Models.
Create group for Views. Drag ContentView.wwift into there.
Create group for App. Drag everything else into there.
Build and show error. Go to Project Settings\Build Settings\Info.plist and upstea to Bullseye/App/Info.plist. Build and run to show all is OK.
Create new file iOS\Swift file. Name it Game.swift.
Update to:
struct Game {
var target: Int = 42
var score: Int = 0
var round: Int = 1
}
Add to ContentView.swift:
@State private var game: Game = Game()
Update target label:
Text(String(game.target))
Remember that every object you create can have data and methods. We’ve figured out the three pieces of data we need for Bullseye, but what methods do we need?
To start, for we just would like a single method, that calculates the points for a given guess. So we could call this method points, and have it take a single parameter which is the slider’s value.
Thsi is the first time we’ve written our own method in Swift, so let me take a moment to explain the synax before we dive in.
Imagine you want to add a method that has no input - in other words, no parameters - but does have an output - in other words, a return value.
To do this, you’d simply add a block of code that looks like this before the final curly brace of your class.
Basically you use the keyword func, then you give your method a name, and then you enter an open and closed parenthesis.
In-between the parenthesis, you put any parameters, or input, that you want your method to accept. The format for this is the parameter name, a colon, then the type of the parameter.
You can create more than one parameter if you’d like; you can just separate them by commas. Or you can have methods that take no parameters - in that case you’d just have an open and close parenthesis with nothing inside.
After your list of parameters, you put a dash and a greater than sign, which looks kinda like an arrow. Then you indicate the type of object that you will return as the output of your method, like Int, Double, or Bool.
Inside the curly braces, you put all of your code. When you’re ready to return the output value, you enter the keyword return, then the value you want to return, like 999, 9.14, or true.
Now that we know what we need, we just need to code it up. So let’s switch back to Xcode!
Add method to Game.swift:
func points(sliderValue: Int) -> Int {
return 999
}
Update alert text:
return Alert(
title: Text("Hello there!"),
message: Text("The slider's value is \(roundedValue).\n" +
"You scored \(self.game.points(sliderValue: roundedValue)) points this round."),
dismissButton: .default(Text("Awesome!")))
Build and run. Show that the label shows 42, and show that is shows 999 for the points.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more