Swift Programming
Swift Programming
Xcode
iOS
Variables and Data Types
Variables allow you to store simple information in your code.
//Variable Syntax Structure
var name: dataType = value
Learning Objectives
In this lesson, you’ll learn:
Table of contents
What are variables
Video Overview
In programming, you can think of a variable as a container that stores simple data for us. This container is given a unique name and whenever we want to use that piece of data in our program, we simply refer to the variable name.
For example:
var number = 100
The variable number
stores the integer value 100.
( We can use Swift’s print
function to view the value stored in number
)
print(number)
// Output: 100
How to create variables
Let’s take a quick look at the syntax structure of creating a variable. (Don’t worry if any of this sounds confusing, we’ll be going through each part one by one).
Video Walkthrough
Here’s the basic syntax structure of a variable
// variable declaration
Var name: dataType = value
Let’s break it down piece by piece:
var
tells the compiler we are creating a variable:var
username
is the unique name we have given our variablevar username
:
is our separator that is placed between our variable name and data type.var username:
String
specifies the type of data this variable will contain:var username: String
=
is our assignment operatorvar username: String =
- And
"Billy"
is the value we assigned to our variable.var username: String = "Billy"
That’s it!
Now everytime we refer to the variable nameusername
, we get returned the String"Billy"
.print(username) // Output: "Billy"
This makes it much more memorable and easy to read as you’ll see when we get to a real world example below.
Deeper look
Keywords
Keywords
such asvar
,func
,switch
are examples of keywords in Swift that are reserved and serve a purpose. Be careful not to use these words when creating variable or argument names as it may result in an error when you try to build your application.
You'll notice the keywords in Swift will turn a specific color that will alert you a keyword is being used. In my example, I am using the midnight dark theme and so my key words show up in Pink. We'll cover the major ones throughout these lessons, so don’t worry about this for now.
Naming Conventions
When creating variable names, we want to put some extra care into choosing a name that is descriptive but also short and concise. Anyone reading your code should immediately know what is being stored.
To help with readability, be sure to use camel casing -- lowercase the first letter of the first word and uppercase any following words in the name for readability. Remember to use camel casing with no whitespace and remember it cannot begin with a number.(e.g. exampleOfCamelCasing)
Data Types in Swift
Swift is a type safe language meaning Swift wants you to be clear of what types of values your code can work with. If you declare your variable to be a data of type Int (which can only store Integers), we cannot pass it a series of text (which is of data type String) without causing an error.
Here are some of the most common data types you’ll find yourself using:
var someInteger: Int = 123
var someString: String = “add some text here”
var someBool: Bool = true
var someDouble: Double = 92.23
var someCharacter: Character = “C”
To view the full list, see Swift’s data types
Using Type Inference
Now the Swift Programming Language supports type inference. Instead of having to manually declare the data type of every variable we create, the compiler is able to figure out on its own if we provide the default value upon creation.
Let’s take a look at an example:
Var temperatureOne: Double = 98.7
// results are the same
Var temperatureTwo = 98.7
print(temperatureOne) //Output: 98.7
print(temperatureTwo) //Output: 98.7
Let’s look at a practical example:
Real World Example:
Let’s say we’re creating an app where the user is requested to enter their:
- “name”
- “age”
- “address”
- select if “new user”
we might create a 4 new variables to store our users information:
userName
of type StringuserAge
of type IntuserAddress
of type StringnewUser
of type Bool
Our code may look something like this:
var userName: String = "James"
var userAge: Int = "22"
var userAddress: String = "123 1st Street"
var newUser: Bool = true
And that’s it!
By now, I hope this lesson gave you a solid understanding of what variables are and more importantly how they can be used when developing your own programs.
What’s Next
In the next lesson, we’ll cover the differences between variables and constants and most importantly the ‘why’ we want to use one over the other.