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
28. Type Inference

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
Learn how something called Swift type inference can help make your code more concise and easy to understand.
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 Type Inference was last updated on Jan 11 2022
We’ve now reduced our points(sliderValue:)
to just a few lines of code.
But good news - there’s a way to reduce the code by a few more characters, thanks to an amazingly handy feature of Swift called type inference. Let’s take a look.
Look at points(sliderValue:)
, and explain type inference
Remove types as follows:
func points(sliderValue: Int) -> Int {
let difference = abs(self.target - sliderValue)
let awardedPoints = 100 - difference
return awardedPoints
}
Remove for properties as well:
var target = Int.random(in: 1...100)
var score = 0
var round = 1
Show how you can option-click to verify that it is correct.
Go to ContentView.swift and update also:
@State private var alertIsVisible = false
@State private var sliderValue = 50.0
@State private var game = Game()
Remove for roundedValue
:
let roundedValue = Int(self.sliderValue.rounded())
Explain how you can get rid of self. in many cases too, and remove it throughout the app.
Go back to Game.swift’s points(sliderValue:) method. Say there’s a way we can simplify this from 3 lines of code down to one.
Replace the line with:
return 100 - abs(self.target - sliderValue)
Then explain how there’s antoher handy thing type inference can do for you. If you have a method that returns a value and only has one line of code, you don’t need to put return. The compiler can infer that since there’s only one line of code, the result of that line will be what is returned from the method. So update the method to this:
100 - abs(self.target - sliderValue)
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