Intro to Swift
🎯 Learning Goals
- Differentiate strings, integers, and doubles
- Create and modify variables
- Use string interpolation to write a sentence using variables
📗 Technical Vocabulary
- Constant
- Double
- Integer
- Interpolation
- Keyword
- String
- Variable
Note: For all practice today, scholars will be working in repl.it.
🌎 Warm Up
Create a list of items you need to complete on the Reminders App on your iPad. This can be a list for today, the week, or the whole summer! Explore the Today
, Scheduled
, and Flagged
features as well.
What is Swift?
Swift is a programming language developed by Apple, specifically for writing applications used on iOS devices. It is responsible for allowing an app to respond to user interaction.
🐣 Discuss
Think back to Reminders App that you explored in the Warm Up, and feel free to reference it while answering these questions.
- When do things change on the screen?
- As a user, are the changes that happen predictable? Why or why not?
- Does more than one thing ever happen at a time?
Takeaways:
- Some of the behaviors of the app: a user can create a list and add as many items on the list as they want. They can also flag specific list items as important or with a specific due date.
- Behaviors all happen after the user has interacted - tapping the empty area to open up the keyboard, swiping a list item left to get the option to flag or delete, etc.
- Some actions result in more than one outcome. When a list item is deleted, it’s removed from the list, and the number next to
All
decreases by one. - All of the behaviors on this app were built by someone who wrote code in the Swift language!
Strings
You can think of strings as a series of characters (alpha, numeric, spaces, and special characters) between two double quote marks, like so:
"Hello, World!"
In Swift, we must use double quotes around any characters that we want to be included in the string.
In order to experiment with things, we’ll open a Swift file in repl.it. Repl.it is a place where we write code just to learn or experiment. The code we write today won’t directly contribute to building an app, but it will help us develop an understanding of the foundations.
Try It: Strings
In your repl.it file, type "your first name"
. Instead of the words "your first name", type your actual name. You do need to include the quotes.
Type "your age"
.
Type "your full name"
.
Type anything you want - try to use some characters from your keyboard that are not numbers or letters. Make sure your text is enclosed in double quotes.
Run the code. If there are any errors in the code you wrote, that line will be highlighted in a light red color, and an error message will appear. Read it and try to understand what the problem is. If you can't resolve it, as another scholar or instructor!
Takeaways:
- Strings can hold any type of character, including spaces
- The way we type a string will be the exact way a computer sees it (it won’t assume we meant to capitalize that first letter, catch if we misspell something, etc.)
Printing
To print values to the console, Swift gives us a handy print()
command. Here’s how it works:
print("Hello, World!")
We would write that line of code in the text editor on the left-hand side portion of the repl.it interface, click the green play button at the top, then see the value printed in the console on the right-hand side.
Variables
In most programming languages, including Swift, values can be saved to variables. Unlike in math class, where we would use x or y and a number, variables in programming are much more flexible. Below are three variables that were used for the Reminders App:
var listName = "Reminders"
var totalReminders = "five"
var reminder = "Take the dogs for a walk"
To define a variable, we use the var
keyword, followed by a name we choose for the variable. Notice that all of the variables start with a lowercase letter. If you’re making a variable name that has two words, uppercase the first letter of the second word. This is called camelCase
.
After naming the variable, we use the =
sign to show what value the variable will hold.
We can now print any of these variables we have defined out to the console. The example below defines three variables, but only one will be logged to the console.
var listName = "Reminders"
var totalReminders = "five"
var reminder = "Take the dogs for a walk"
print(listName)
Try It: Strings and Variables
Complete the following in the same repl.it file. You should type your code in the Swift editor on the left-hand side, and see the results in the console on the right-hand side.
Declare a variable for each prompt below. Then, print it out to the console.
- A variable called
favoriteColor
that holds your favorite color - A variable called
pet
that holds the name of a pet - A variable called
friend
that holds the name of a friend - A variable called
goal
that holds one of your 2020 goals. Remember, you can include spaces in a string! - A variable called
hobby
that holds one of your hobbies
Keywords are special, reserved words in the language. Every programming language has them. Xcode is helpful because it shows all keywords in pink.
Re-assigning Variables
Sometimes, things in life change! We might change our names, move cities, our ages will almost definitely change, etc. In the Reminders App, users have the option to change the name of a list.
Swift gives us the ability to re-assign a variable so that its value can change. Here is the syntax:
var listName = "Reminders"
listName = "Monday To-Dos"
print(listName)
//=> Monday To-Dos
Notice that when we re-assign a variable, we do not use the keyword var
.
Constants
If we want to store information that we know will NEVER change, use the let
keyword to declare a constant.
let name = "Karlie"
We can still access the string “Karlie” anytime, we just can’t re-assign it.
🗂 Brainstorm: Uses for Constants
Come up with 3-4 real life uses for a constant, a piece of data that will never change. Be ready to share out!
Are there any places where a constant would be appropriate for the Reminders App?
Declaring Variables with No Value
In other languages, you may have seen variables declared without being assigned a value. To do that in Swift, we have to tell the program what data type it should be.
var name : String
var lastNumber : Int
Maybe I don’t know the name or last number yet, but Swift requires that I declare which type of data will later be stored in it. var name
alone will NOT work.
Once I do have the data I’d like to store, I can re-assign the variable like this:
var name : String
var lastNumber : Int
name = "Karlie"
lastNumber = 78
String Interpolation
We can also include variable data in a sentence. This is called interpolation:
var listName = "Reminders"
var totalReminders = "five"
print("There are \(totalReminders) items on the \(listName) list.")
//=> There are five items on the Reminders list.
The computer reads anything inside of the \( )
as Swift code. In the code snippet above, the string "There are "
was printed, then the program saw \(
and read totalReminders
as a variable. Instead of printing out the word totalReminders
, it substituted it’s stored value of “five” instead. Once the program read the closing )
, it printed out " items on the "
, then continued with the same process for the second variable.
Try It: Strings
Declare the following variables in your repl.it file:
var first = "Karlie"
var last = "Kloss"
Use string interpolation to complete the following:
- Output the string
"KarlieKloss"
. - Output the string
"KlossKarlie"
. - Output the string
"Karlie Kloss"
. - Output the string
"Kloss Karlie Kloss Karlie"
. - Output the string
"I love Karlie"
.
Numbers
We will use two kinds of numbers - Integers and Doubles. The math operations we use all the time can be used on both of these. Also, the Order of Operations applies to math in code!
Integers
Like we saw with strings above, we can also store Integers in variables.
var totalReminders = 5
var today = 2
var scheduled = 3
var flagged = 1
We can also re-assign variables that store numbers. In the Reminders App, this is how the program keeps track of the total number of reminders on a given list.
var totalReminders = 0
// The line below re-assigns points to its previous value (0) plus 1.
totalReminders = totalReminders + 1
// => 1
// The line below re-assigns points to its previous value (1) plus 1.
// It is a shortcut that does the exact thing as the example above!
totalReminders += 1
// => 2
🐣 Discuss: Incrementing Numbers
Incrementing numbers like what we just looked at above is something developers do very frequently.
With your partner, brainstorm some examples of numbers incrementing or decrementing in real apps that you use.
Takeaways:
- On a users birthday, an app should increment their age
- Social media apps use incrementing to keep track of the number of likes, replies, followers, etc.
- Apps that involve scheduling - calendar events, flights, live video classes, etc. usually have a countdown timer to keep the user informed on the amount of time until something will happen
Integers in Action
Like we did with strings, we can interpolate with variables that hold numbers.
var listName = "Reminders"
var totalReminders = 5
totalReminders += 1
print("There are \(totalReminders) items on the \(listName) list.")
//=> There are 6 items on the Reminders list.
We can do math with numbers in Swift! The same math operators you are used to from math-class work here.
3 + 5
//=> 8
12 / 2
//=> 6
18 - 8
//=> 10
We can also use parenthesis, and the Order of Operation holds true here as well.
(3 + 2) * 8
// 5 * 8
//=> 40
We can also use math on variables if those variables hold numbers.
var name = "Karlie"
var tinsOfKookies = 1200
var mealsDonatedPerTin = 10
var mealsDonated = tinsOfKookies * mealsDonatedPerTin;
print(mealsDonated);
//=> 12,000
print("Because \(tinsOfKookies) tins of kookies were purchased during Fashion's Night Out, \(mealsDonated) meals were donated to starving children all over the world. Thanks, \(name)!")
//=> "Because 1200 tins of kookies were purchased during Fashion's Night Out, 12,000 meals were donated to starving children all over the world. Thanks, Karlie!"
Try It: Integers & Operators
Start with these numbers:
var januaryAvg = 12 var marchAvg = 65 var novemberAvg = 31 var julyAvg = 98
- Write code to find the average of these four temperatures.
- Find the average yourself using paper or a calculator. Is your answer different than you found with Swift? Why might that be?
- Say you have the operation
januaryAvg + marchAvg * novemberAvg / julyAvg
. What result do you get out from Swift? What other outputs can you get out by adding one or more pairs of parentheses to the expression?
Medium Challenge: Find the answer to this problem using Swift: On average, there are 24 scholars at each Kode With Klossy camp this year. If there are 36 camps taking place, about how many scholars are attending in total? Print out to the console your answer in a complete sentence.
Doubles
In the Try It section above, Swift calculated the average of the four temperatures to be 51
, even though we can verify on a calculator that the more precise answer is 51.5
. This is because if we ask Swift to perform an operation on two Integers, it will result in an Integer. From our example above, we can see that it rounded down.
What if we wanted a more precise answer? We would need to use a Double.
Swift gives you two data types with which to store numbers that have a decimal - Floats and Doubles. They can be positive or negative. Examples would be 1.1
, 42.45
, 3.14
, -123786234.64
. There are some technical differences between the two, but for our purposes this summer, if you ever need a decimal use a Double because you’ll get more accuracy that way.
var januaryAvg = 12
var marchAvg = 65
var novemberAvg = 31
var julyAvg = 98
var total = (januaryAvg + marchAvg + novemberAvg + julyAvg)
Double(total) / 4.0
=> 51.5
On the last line of code in the snippet above, we changed two key things:
- We converted the total of the 4 temperatures into a double with the
Double(total)
syntax. That changed206
to206.0
. It seems like a small difference to us, but it is a big difference to Swift. - We divided by
4.0
, a Double, instead of4
, an Integer. We did this for consistency.
We can’t use all math operations on a double and an integer. Why? They are technically different data types. Even though they all look like numbers to us - they are stored differently. To use most math operations on an Integer with a Double, we would convert the integer into a double.
Example:
var integer = 4
var double = 5.0
integer * double --> ERROR
Double(integer) * double --> 20.0
NOTE: Some math operations will work with a mix (for example: 206.4 / 4), but since that doesn’t work on most operations, it’s standard to always use the same data type when performing any math operation.
Swift Data Types
Let’s take a few minutes to practice these fundamentals - we will use this knowledge every time we work on a project from here on out!
Practice: Data Types
For each item on the list below, determine if it should be stored as a variable or constant. Be ready to explain your thinking.
name
address
cityBornIn
gradeLevel
birthday
age
eyeColor
favoriteColor
Now, declare variables or constants for each of the items listed above, and assign an appropriate value.
Lastly, use string interpolation to write at least 3 different sentences about yourself, using at least one variable in each sentence.
Click here for a Mild Challenge 🌶
For each item on the list below, determine what data type would be appropriate.
collegeAttended
numberOfPlacesTraveled
bestFriend
numberOfCitiesLivedIn
Now, declare variables or constants for each of the items listed above, but don't assign them a value yet.
Click here for a Medium Challenge 🌶🌶
Find the answer to this problem using Swift:
Karina earns $10.25 per hour at her job at Express. If she worked 20 hours last week, how much should she get paid? Print a complete sentence out with the total.
Click here for a Spicy Challenge 🌶🌶🌶
There's a silly compression algorithm that outputs the first letter, the number of letters in the middle, and the last letter. So for the string Klossy
it'd output K4y
or scholar
would be s5r
.
Can you write code to implement that? Hint: You'll probably need to use your research skills to find something that will help!