5-Day Swift Coding Challenge: Day 1

Nov 17 2025 · Swift 6.0, iOS 18, Swift Playground 4.6

Lesson 01: Day 1: Meet the Swift Programming Language

Demo: Variables in Action

Episode complete

Play next episode

Next
Transcript

To get started, open up your Swift Playgrounds app. You’ll be presented with a few options to create a new playground. If you are on an iPad, tap the New Playground option and select the New Book option. On macOS, at the time, we don’t have access to create a new book. Instead, we need to do this from the menubar. Select File and the New Book option.

You’ll see the playground listed in the sidebar. Right click and select the Open option. You’ll now see your empty playground.

It’s a convention to print a “Hello World” message to the screen. Now, the default text on macOS is pretty small. You can increase the size by pressing Command-Plus to increase the font size. Of course, Command-Minus to decrease the font size.

Now to create your first variable. Type the following:

var greeting = "Hello World!"

Here you created a new variable called greeting. This variable contains text. In code, this is known as a string like a string of characters. Now, print it to the screen. Add the following:

print(greeting)

Print is known as a function. This allows you to show a message on the screen. Now, press the Run My Code button. You’ll notice nothing happens, but look down in the lower-right hand corner. An icon has a red notification bubble. This is the console icon. Click on it. This shows the console. It displays the output of your code. Mind you, most code will not print messages to the console.

Now change your code to the following:

print(greet)

Run your code. You’ll see your first error message. You are using a variable you haven’t defined. Nothing is printed out to the console. This is a compile error. You can’t run the program until you fix it. Compile errors are part of the process. Compile errors are not indicative of your programmer skill. It’s just a mistake. If you aren’t making errors, you aren’t coding.

Okay, fix the error by adding the proper variable.

print(greeting)

The fun thing about strings is you can add emojis to them. Let’s create a traffic light. Use the green circle, yellow circle, and red circle. One nice thing about the emoji window is you can click on it and dettach it. This is useful when working with lots of them.

print("🟢")
print("🟡")
print("🔴")

You’ve created a traffic light. Run your code. Look at that, it’s printed on the screen. You can also put your drawing skills to the test. Do the following:

print("🟧🟧🟧")
print("🟧🔴🟧")
print("🟧🟡🟧")
print("🟧🟢🟧")
print("🟧🟧🟧")

You’ll see I employed a lot of copying and pasting. When coding, look for strategies to save time. Now run your code. You’ll see your traffic light.

Now try printing out your name. Create a new variable for your name.

var firstName = "Brian"

Next for a hello message with your name added to it. You’ll put your name in another string. Add the following:

greeting = "Hello \(firstName)"
print(greeting)

A few things are happening. First, you are reusing the greeting variable you defined earlier. Next, you are putting your variable between a backslash parenthesis. This is known as string interpolation. You are printing your variable to the string. Run your code. You’ll see your name.

Mind you, you can put other Swift code between the parenthesis. The code is evaluated and added to your existing string. For example:

greeting = "Hello \(1000 * -20)"

For now, you’ll just print out variables. Okay, take a moment for a quick challenge. Create a new variable called lastName to store your last name. Next print out your first and last name. Also for the purposes of this demo, make your last name variable a constant. Pause the video. Okay, hopefully, that went well. This is what I did.

let lastName = "Moakley"
print("Hello \(firstName) \(lastName)")

Don’t worry if your code didn’t match my code. There are lots of ways to do the same thing with code. Focus on the end result for now. Run your code. And you should see your name. Did you use the let keyword? Remember, with constants, I can’t change the result. For example,

lastName = "Einstein"

You’ll see I get a few errors. I can’t change my last name to Einstein. Okay, I can fix this by deleting it, but sometimes, you can also turn it into a comment. Add two forward slashes before the code.

This tells Swift to ignore the line. This is useful for providing documentation inside your code.

Okay, so far you’ve seen string variables. Here are some others.

var number = 10
var decimal = 10.4
var trueOrFalse = true
var emoji = "🐰"

Here you’ve defined an integer, a double, a bool, and a character. The integer is always a whole number. If you divide seven by 2, you’ll get an answer of 3. The integer does not store any decimal point information. That’s the job of the double. The Bool contains just true or false values. Finally, the character represents a just single character. It looks like a string, but it isn’t.

To really signify the types, update them to the following:

var number: Int = 10
var decimal: Double = 10.4
var trueOrFalse: Bool = true
var emoji: Character = "🐰"

Finally, add a string

var text: String = "Learning Swift rocks!"

Most of the time, you’ll be working with strings as opposed to characters. Finally, there are times when you need to combine data types. Add the following:

let myInt = 10
let myDouble = 20.0
let myTotal = myInt + myDouble

You’ll get an error. You can’t combine different types. You need to convert them to the same type. In the case of the double, convert it an int.

let myTotal = myInt + Int(myDouble)
print(myTotal)

Run your code. Notice the floating point information was dropped. If you needed to keep that information, you would convert the int to a double.

let myTotal = Double(myInt) + myDouble

Often times, conversions otherwise known as casting, can drop data. This is why you have to opt-into it. For now, just put that fact in your toolbox. You won’t be doing any type conversion in this course, but keep it in mind for your overall swift learning journey.

See forum comments
Cinema mode Download course materials from Github
Previous: Swift Variables & Data Types Next: Working with Objects