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 3: Coding in Swift
27. Variables & Constants

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
Finalize the difference algorithm and score calculation, and learn about an important Swift concept: the difference between variables and constants.
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 Variables & Constants was last updated on Jan 11 2022
At this point, you’ve simplified your algorithm to calculate the difference down to 3 lines of code.
That’s good, but we can do even better; we can get it down to one line of code!
In this excercise, we’ll finialize our revisions to the difference algorithm, and put it all together to calculate the score and display it to the user. Along the way, you’ll learn about an important Swift concept: the difference between variables and constants.
Let’s dive in!
Help\Developer Documentation. Swift Standard Libary\Int. Halfway down, find section “Finding the Sign and Magnitude”. Abs function.
Alternative:
var difference: Int = abs(self.target - sliderValue)
Explain we’ve been getting warnings, they say ‘consider changing to let constant’, and you might be wondering why that is. That leads me to an imporant discussion, about the let vs. var.
It turns out Swift makes a distinction between variables and constants.
As we discussed in the episode on Variables, you can change the value of a variable at any time. However, once you set a constant, you can never change it again. If you try to change it, the Swift compiler will give you an error.
The keyword var creates a variable, while let creates a constant.
In the first versions of your algorithm, you declared the difference as a variable, because the value could change depending on you if/else statement. But in the latest version, once you calculate the difference, or the awardedPoints, they will never change again. So they could really be a let - or a constant, rather than a variable.
When you declare a value that will never change in Swift, it’s better to make it a constant with let. This makes your intent clear, which in turn helps the Swift compiler understand your program better.
A good rule of thumb is prefer to use constants with let as much as possible, and only use variables with var if you need to change the value later on. That way, the Swift compiler can optimize your code as much as possible.
That’s why we’ve been seeing these warnings in Bulleye so far. It’s because Xcode noticed that in some cases, we have been using var to declare variables, but those variables never changed, so we could have used let - and it helpfully notified us about that.
So let’s go through and update those variables to constants, which should resolve all our warnings and get Bullseye in a clean state.
Game.swift; Lines 16+
let difference: Int = abs(self.target - sliderValue)
let awardedPoints: Int = 100 - difference
ContentView.swift; Line 67
let roundedValue: Int = Int(self.sliderValue.rounded())
Unit tests
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