From the course: Swift 6 Essential Training
Unlock this course with a free trial
Join today to access over 24,700 courses taught by industry experts.
Variables and constants - Swift Tutorial
From the course: Swift 6 Essential Training
Variables and constants
- [Instructor] All right, if you have the Chapter 2 Playground from the AdventureSwift working folder on screen, click on the variables and constants page. If not, go ahead and open that up, and let's talk about variables. So in Swift, you have two primary options for declaring variables, depending on how you plan to use them. Variables that need to be changed after they're created start with the VAR keyword, which tells the Swift compiler the value can be modified. Now to see this in action, let's complete our first objective and declare a changeable variable to store a player's health and assign it a value of 90. Start with the VAR keyword. We'll call this currentHealth. We just use the equals symbol to assign it a value. Now, Swift does encourage the use of camel case for names. So we have the first letter of our variable name lowercased, and then all following first letters are in caps. You'll also notice that we…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
The quest begins: Variables34s
-
(Locked)
Variables and constants3m 6s
-
Type safety and inference3m 11s
-
(Locked)
Logging and commenting1m 58s
-
(Locked)
Swift operators2m 42s
-
(Locked)
Understanding strings5m 13s
-
(Locked)
Working with strings5m 11s
-
(Locked)
Type conversions4m 57s
-
(Locked)
Booleans and logical operators2m 40s
-
Introducing optionals3m 15s
-
(Locked)
Accessing Documentation1m 26s
-
(Locked)
Challenge: Player stats45s
-
(Locked)
Solution: Player stats4m 12s
-
(Locked)
-
-
-
-
-
-