WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Why does Swift have variables?

Paul Hudson    @twostraws   

Updated for Xcode 14.2

Variables allow us to store temporary information in our program, and form a key part of almost every Swift program. Ultimately, your program is going to transform data somehow: maybe you let the user enter in todo list tasks then check them off, maybe you let them roam around a deserted island working for a capitalist raccoon, or maybe you read the device time and display it in a clock. Regardless, you’re taking some sort of data, transforming it somehow, and showing it to the user.

Of course, the “transforming it somehow” is where the real magic comes in, because that’s the part where your brilliant app idea happens. But the process of storing data in memory – holding on to something the user typed, or something you downloaded from the internet – is where variables come in.

Once you create a variable using var, you can change it as often as you want without using var again. For example:

var favoriteShow = "Orange is the New Black"
favoriteShow = "The Good Place"
favoriteShow = "Doctor Who"

If it helps, try reading var as “create a new variable”. So, the first line above might be read out loud as “create a new variable called favoriteShow and give it the value Orange is the New Black.” Lines 2 and 3 don’t have var in there, so they modify the existing value rather than creating a new variable.

Now imagine you had var on all three lines – you used var favoriteShow each time. That wouldn’t make much sense, because you’d be saying “create a new variable called favoriteShow” three times over, and the variable is clearly not new after your first attempt. Swift will flag this as an error, which means it won’t let you run your code until you pick a different name for your variables.

That might seem like annoying behavior, but trust me: it’s helpful! Swift wants you to be clear: are you trying to modify an existing variable (if so, remove the var the second and subsequent times), or are you trying to create a new variable (in which case, name it something else.)

One last thing: although variables form the core of many Swift programs, you will learn that sometimes they are best avoided. More on that later!

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.6/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.