Well done!
You have completed Hello Swift & SwiftUI!
You have completed Hello Swift & SwiftUI!
Learn Swift programming using an Xcode Playground. In this video, you'll learn the essentials—from declaring variables and constants to mastering string interpolation and generating random numbers.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upHi and welcome back! 0:00 Now that you have Xcode installed, it's time to start coding in Swift. 0:01 In this video, I'll introduce you to the fundamentals of Swift, including 0:06 how to write basic code, use variables, and work with common data types. 0:10 These building blocks will set you up for success as we move 0:15 towards creating your very first app by the end of this workshop. 0:18 Let's jump in! 0:23 To get started, let's open Xcode. 0:25 Instead of creating a full app project, we'll use a playground, 0:29 a space where you can quickly write and test Swift code. 0:33 To create one, go to the top left and click 0:37 File, New, Playground. 0:40 When prompted to choose a template, select macOS Blank. 0:43 This is the simplest option and perfect for practicing Swift. 0:48 Next, name your file whatever you'd . 0:52 I'll name mine Swift-Basics and save it in my Swift folder. 0:55 But you can save yours anywhere that's convenient. 1:00 Once your 1:04 playground is set up, you'll see a window with three panels. 1:04 The left panel is a navigator. 1:08 Since we won't be using it in this workshop, 1:10 you can hide it by clicking this icon. 1:13 The right panel is where we'll see the output of our code as it runs. 1:16 We'll explore this in action soon. 1:20 The center panel is where we'll be writing our Swift code. 1:23 This is where the magic happens. 1:27 When you open your Playground, Xcode provides some sample code to start with. 1:29 The first line imports the Cocoa framework, which includes 1:34 libraries and APIs for macOS app development. 1:38 Since we won't be using it in this workshop, we can safely delete that line. 1:42 Now let's focus on the second line. 1:47 var greeting equals hello Playground. 1:49 here's what's happening var is a keyword that creates a variable 1:52 which is a named storage for data that can change 1:57 greeting is the name of our variable 2:01 and it's set to the string hello comma playground 2:04 which is a sequence of text in close in quotation marks 2:08 variables are a fundamental part 2:12 of programming so let's explore that further in swift 2:15 you use var to declare variables and let to declare constants. 2:19 Here's an example. 2:24 I'll create a variable called name and set it equal to my name, Laura. 2:26 Then I'll create a constant named color and set it to red. 2:31 Now let run our code Click the play button next to the line number 2:36 and Xcode will execute everything up to that point 2:40 The first time you run it it may take a moment but once it finished 2:44 you see the values of our variable and constant displayed in the right panel 2:48 So what's the difference between var and let? 2:53 Variables declared with var can have their values changed later. 2:57 Constants declared with let cannot be changed once they're assigned a value. 3:02 Let me show you this in action. 3:08 I'll update the name variable's value 3:10 to Chris by writing name equals Chris. 3:13 Notice that I didn't use var again. 3:18 That's because you only use var or let 3:20 when declaring a variable or constant for the first time. 3:23 After that, you can simply update the value for variables. 3:27 Now let's run our code. 3:31 In the right panel, you'll see that the name variable's value 3:33 has updated to Chris. 3:37 But what happens if we try to change the value of a constant? 3:39 Let's test it out. 3:43 I'll attempt to change color to blue. 3:45 After a moment, we get an error. 3:48 Cannot assign to value. 3:50 Color is a let constant. 3:52 This means Swift won't let us reassign a new value to color 3:55 because it was declared as a constant using let. 3:59 Constants are useful when you want to ensure 4:03 a value remains unchanged throughout your program. 4:05 To fix this, let's delete the line that tries to change color 4:09 and the error will disappear. 4:13 Swift is a type 4:16 safe language, which means that once a variable has been assigned a type, 4:17 it cannot change to another type. 4:22 Let's test this by trying to assign a number to our name variable. 4:25 I'll attempt to set name to 42. 4:30 Right away, we get an error. 4:34 Cannot assign value of type int, 4:36 which stands for integer, to type string. 4:39 This tells us that name was originally declared as a string, 4:43 so it can only store text, not numbers. 4:47 In Swift, once a variable has a type, 4:50 it must always hold values of that same type. 4:53 Let's delete this line to remove the error. 4:57 Swift supports many data types, but in this introductory workshop, 5:01 we'll focus only on the ones we'll be using for our Dice Roller app. 5:06 The first type, 5:11 which we've been using from the start, is the string type. 5:12 Strings are used for text and are enclosed in quotation marks 5:16 Another data type we briefly saw is integers which represent 5:21 whole numbers Let create a variable that stores an integer 5:25 var myAge equals 30 5:30 Unlike strings you don need quotation marks when assigning an integer. 5:34 Now that we know how to store data in variables, what can we do with it? 5:38 One useful thing we can do is print values 5:43 to the console using the print function. 5:46 The print function allows us to send messages 5:49 to the console which appears at the bottom of the screen. 5:51 Let's try printing a message. 5:55 Inside of this print statement, let's write a string, my name is. 5:58 We can even print a variable. 6:03 Now let's run the file and check the console. 6:06 We see two messages, my name is and Laura. 6:10 The first message comes from the string we wrote inside the print function, 6:15 and the second message comes from our name variable. 6:20 Swift recognizes the variable, 6:24 retrieves its stored value, and prints it to the console. 6:26 Printing is a great way to check values 6:30 and debug your code as you write it. 6:32 Let's experiment by switching the order of our print statements. 6:36 I'll use Command X to cut the first print statement 6:40 and Command V to paste it below the second one. 6:43 Programming is all about experimenting 6:48 and seeing what happens, so don't be afraid to play around. 6:51 All great programmers do. 6:54 Now let's run the code. 6:56 As expected, the message appears in the new order. 6:58 That's because in Swift and most programming languages, 7:01 code runs from top to bottom, 7:05 executing each line in sequence. 7:08 The first print statement runs first, followed by the next, and so on. 7:11 What if 7:16 we wanted both messages to appear on the same line? 7:17 Instead of using two separate print statements, 7:20 we can combine them using string interpolation. 7:23 String interpolation allows us to insert variables inside 7:27 a string by using a backslash followed by parentheses. 7:31 Let's also have the message display our age. 7:37 And I am backslash open parentheses 7:40 my age years old. 7:43 Now if we delete the previous 7:47 print statement and run the code, we see 7:48 my name is Laura and I am 30 years old. 7:51 Everything is printed on a single line. 7:55 String interpolation makes code more dynamic 7:58 and readable For example if we update our variables 8:01 we can reassign the name variable to John 8:06 and set his age to 12 8:09 and run the code again Now we see 8:13 my name is John and I am 12 years old. 8:16 This shows how powerful variables are. 8:20 They allow us to write flexible, reusable code. 8:23 The last topic I want to cover 8:27 is a key feature for our Dice app, generating random numbers. 8:29 When you roll a standard die, you get a number between 1 8:34 and 6, and you never know what the number will come up. 8:37 It's random. 8:41 Similarly, in coding, when you need a random number, 8:42 you can tell Swift the range, for example 1 to 6, 8:45 and Swift will randomly select the number for you. 8:49 In Swift, this is done using the following code. 8:53 Let dice roll equals int dot random 8:57 in 1 dot dot dot 6. 9:01 Let's break it down. 9:05 Int specifies that we want a whole number, an integer. 9:06 Dot random in tells Swift, give me a random number. 9:10 1 dot dot dot 6 defines the range. 9:15 It tells Swift, choose a number between 1 9:19 and 6, including both 1 and 6. 9:22 Let dice roll 9:27 stores a random number in a constant, so we can use it later. 9:28 Without storing it, we wouldn't be able to reference it in our program. 9:32 Now let's say you're making a board game app and need to simulate rolling two dice. 9:37 You could write let first dice equal 9:42 int dot random in 1 dot dot dot 6. 9:46 Then copy it, paste it, 9:52 and we'll rename this one to second dice. 9:55 Then have a print statement that says you rolled 10:00 a backslash first dice 10:04 and a backslash second dice. 10:08 If we run the code, we see 10:12 you rolled a 5 and a 1. 10:15 If we run it again, we should see different results 10:19 each time, just rolling real dice. 10:22 Great work! 10:27 You've now learned 10:27 some key Swift fundamentals that we'll use when building our Dice Roller app. 10:28 You now know how to work with variables and constants, 10:33 use string interpolation, and generate random numbers. 10:37 In the next video, I'll give you a tour of SwiftUI. 10:41 I'll see you there. 10:44
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up