diff --git a/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift index cd86d9d..d2f4b79 100644 --- a/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift @@ -10,14 +10,15 @@ print("How to use playgrounds to make writing Swift fun and simple") /*: Now print your own phrases to the console. Pick one of your favorite songs. Use your knowledge of the `print` function to display the song title and artist. */ - - +print("Title | Alive") +print("Artist | David Guetta feat. Sia ") /*: Use multiple `print` functions to write out some of the lyrics to the song. */ - - - +print("I'm bulletproof nothing to lose") +print("Ricochet, you take your aim") +print("You shoot me down but I won't fall") +print("I am titanium") /*: _Copyright © 2018 Apple Inc._ diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift index 8721318..1060a22 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift @@ -1,18 +1,18 @@ /*: ## Exercise - Constants - + Declare a constant called `friends` to represent the number of friends you have on social media. Give it a value between 50 and 1000. Print out the value by referencing your constant. */ - - +let friends = 50 +print(friends) /*: Now assume you go through and remove friends that aren't active on social media. Attempt to update your `friends` constant to a lower number than it currently is. Observe what happens and then move to the next step. */ - - +// Cannot assign to value: 'friends' is a 'let' constant +//you can't change a costant. /*: Does the above code compile? Why not? Print your explanation to the console using the `print` function. Go back and delete your line of code that updates the `friends` constant to a lower number so that the playground will compile properly. */ - - +//No +print("compile error occurs because 'friends' is a 'let' which is constant value, if you are willing to change it, it supposed to be 'var' ") //: page 1 of 10 | [Next: App Exercise - Step Goal](@next) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift index 21166b7..7241227 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift @@ -1,16 +1,16 @@ /*: ## App Exercise - Percent Completed - + >These exercises reinforce Swift concepts in the context of a fitness tracking app. - + You decide that your fitness tracking app should show the user what percentage of his/her goal has been achieved so far today. Declare a variable called `percentCompleted` and set it to 0. Do not explicity assign it a type. */ - +var percentCompleted : Double = 0 /*: Imagine that partway through the day a user has taken 3,467 steps out of the 10,000 step goal. This means he/she is 34.67% of the way to his/her goal. Assign 34.67 to `percentCompleted`. Does the code compile? Go back and explicity assign a type to `percentCompleted` that will allow the code to compile. */ - +percentCompleted = 34.67 /*: diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift index b71a821..54ef0ba 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift @@ -5,11 +5,11 @@ Your fitness tracking app needs to know goal number of steps per day. Create a constant `goalSteps` and set it to 10000. */ - +let goalSteps = 10000 /*: Use two `print` functions to print two separate lines to the console. The first line should say "Your step goal for the day is:", and the second line should print the value of `goalSteps` by referencing your constant. */ - - +print("Your step goal for the day is:") +print(goalSteps) //: [Previous](@previous) | page 2 of 10 | [Next: Exercise - Variables](@next) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift index 1b6fe9b..2c37a20 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift @@ -3,16 +3,16 @@ Declare a variable `schooling` and set it to the number of years of school that you have completed. Print `schooling` to the console. */ - - +var schooling = 16 +print(schooling) /*: Now imagine you just completed an additional year of school, and update the `schooling` variable accordingly. Print `schooling` to the console. */ - - +schooling += 1 +print(schooling) /*: Does the above code compile? Why is this different than trying to update a constant? Print your explanation to the console using the `print` function. */ - +print("yes, the variable can be changed if you want while the constant can't ") //: [Previous](@previous) | page 3 of 10 | [Next: App Exercise - Step Count](@next) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift index 4c95fb5..042a3f8 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift @@ -5,11 +5,12 @@ Create a variable called `steps` that will keep track of the number of steps you take throughout the day. Set its initial value to 0 to represent the step count first thing in the morning. Print `steps` to the console. */ - - +var steps = 0 +print(steps) /*: Now assume the tracker has been keeping track of steps all morning, and you want to show the user the latest step count. Update `steps` to be 2000. Print `steps` to the console. Then print "Good job! You're well on your way to your daily goal." */ - - +steps = 2000 +print(steps) +print("Good job! You're well on your way to your daily goal.") //: [Previous](@previous) | page 4 of 10 | [Next: Exercise - Constant or Variable?](@next) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift index 1d38a49..fa96775 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift @@ -2,17 +2,17 @@ ## Exercise - Constant or Variable? Imagine you're creating a simple photo sharing app. You want to keep track of the following metrics for each post: -- Number of likes: the number of likes that a photo has received -- Number of comments: the number of comments other users have left on the photo -- Year created: The year the post was created -- Month created: The month the post was created represented by a number between 1 and 12 -- Day created: The day of the month the post was created + - Number of likes: the number of likes that a photo has received + - Number of comments: the number of comments other users have left on the photo + - Year created: The year the post was created + - Month created: The month the post was created represented by a number between 1 and 12 + - Day created: The day of the month the post was created For each of the metrics above, declare either a constant or a variable and assign it a value corresponding to a hypothetical post. Be sure to use proper naming conventions. */ - - - - - +var numberOfLikes = 44 +var numberOfComments = 17 +let yearCreated = 2020 +let monthCreated = 6 +let dayCreated = 6 //: [Previous](@previous) | page 5 of 10 | [Next: App Exercise - Fitness Tracker: Constant or Variable?](@next) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift index a9bfc4b..92e8948 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift @@ -11,11 +11,16 @@ - Goal number of steps: The user's goal for number of steps to take each day - Average heart rate: The user's average heart rate over the last 24 hours */ - - - - - +let name = "Norah" +print("Name won't be changed so I declared it as a constant") +var age = 21 +print("Age is gonna change every year so I declared it as a variable") +var numberOfStepsToday = 5000 +print("Steps are going to change everyday so I declared it as a variable") +let goalNumberofSteps = 1200 +print("The goal number of steps won't be changed so I declared it as a constant") +var averageHeartRate = 58 +print("The average heart rate is gonna be updated so I declared it as a variable") /*: Now go back and add a line after each constant or variable declaration. On those lines, print a statement explaining why you chose to declare the piece of information as a constant or variable. */ diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift index 6a26c32..67b8e93 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift @@ -3,21 +3,23 @@ Declare two variables, one called `firstDecimal` and one called `secondDecimal`. Both should have decimal values. Look at both of their types by holding Option and clicking on the variable name. */ - - +var firstDecimal = 11.1 // Double +var secondDecimal = 111.11 // Double /*: Declare a variable called `trueOrFalse` and give it a boolean value. Try to assign it to `firstDecimal` like so: `firstDecimal = trueOrFalse`. Does it compile? Print a statement to the console explaining why not, and remove the line of code that will not compile. */ - - +var trueOrFalse = true +//firstDecimal = trueOrFalse +print("there is a mismatch between the types, you cannot assign value of type 'Bool' to type 'Double'") /*: Declare a variable and give it a string value. Then try to assign it to `firstDecimal`. Does it compile? Print a statement to the console explaining why not, and remove the line of code that will not compile. */ - - +var string = "" +//firstDecimal = string +print("there is a mismatch between the types, you cannot assign value of type 'String' to type 'Double'") /*: Finally, declare a variable with a whole number value. Then try to assign it to `firstDecimal`. Why won't this compile even though both variables are numbers? Print a statement to the console explaining why not, and remove the line of code that will not compile. */ - - -//: [Previous](@previous) | page 7 of 10 | [Next: App Exercise - Tracking Different Types](@next) +var number = 5 +//firstDecimal = number +print("Even though they are all numbers but number is an Int and firstDecimal is a Double and you cannot assign value of type 'Int' to type 'Double'") diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift index 1badf6b..b292a8a 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift @@ -5,11 +5,11 @@ You have declared a number of constants and variables to keep track of fitness information. Declare one more variable with a boolean value called `hasMetStepGoal`. */ - +var hasMetStepGoal = true /*: When you declared a constant for goal number of steps and a variable for current step count, you likely assigned each a value in the thousands. This can be difficult to read. Redeclare this constant and variable and, when assigning each a value in the thousands, format the number so that it is more readable. */ - - +var numberOfStepsToday = 5_000 +let goalNumberofSteps = 1_2000 //: [Previous](@previous) | page 8 of 10 | [Next: Exercise - Type Inference and Required Values](@next) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift index 5c81e84..4392f48 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift @@ -3,21 +3,21 @@ Declare a variable called `name` of type `String`, but do not give it a value. Print `name` to the console. Does the code compile? Remove any code that will not compile. */ - - +var name: String +//print(name) /*: Now assign a value to `name`, and print it to the console. */ - - +name = "Norah" +print(name) /*: Declare a variable called `distanceTraveled` and set it to 0. Do not give it an explicit type. */ - +var distanceTraveled : Double = 0 /*: Now assign a value of 54.3 to `distanceTraveled`. Does the code compile? Go back and set an explicit type on `distanceTraveled` so the code will compile. */ - +distanceTraveled = 54.3 //: [Previous](@previous) | page 9 of 10 | [Next: App Exercise - Percent Completed](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift index 4cbbad4..5924d70 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift @@ -3,27 +3,33 @@ You decide to build a shed and want to know beforehand the area of your yard that it will take up. Create two constants, `width` and `height`, with values of 10 and 20, respectively. Create an `area` constant that is the result of multiplying the two previous constants together, and print out the result. */ - - + let width = 10 + let height = 20 + let area = width * height + print(area) /*: You decide that you'll divide your shed into two rooms. You want to know if dividing it equally will leave enough room for some of your larger storage items. Create a `roomArea` constant that is the result of dividing `area` in half. Print out the result. */ - - +let roomArea = area/2 +print(roomArea) /*: Create a `perimeter` constant whose value equals `width` plus `width` plus `height` plus `height`, then print out the result. */ - - +let perimeter = width + width + height + height +print(perimeter) /*: Print what you would expect the result of integer division of 10 divided by 3 to be. Create a constant, `integerDivisionResult` that is the result of 10 divided by 3, and print the value. */ - - +print(3) +let integerDivisionResult = 10/3 +print(integerDivisionResult) /*: Now create two constants, `double10` and `double3`, set to 10 and 3, and declare their types as `Double` values. Declare a final constant `divisionResult` equal to the result of `double10` divided by `double3`. Print the value of `divisionResult`. How does this differ from the value when using integer division? */ - +let double10: Double = 10 +let double3: Double = 3 +let divisionResult = double10/double3 +print(divisionResult) /*: Given the value pi (3.1415927), create a `radius` constant with a value of 5.0, then calculate the diameter and circumference of the circle using the following equations, and print the results: @@ -33,6 +39,10 @@ *circumference = 2 * pi * radius.* */ let pi = 3.1415927 - +let radius = 5.0 +let diameter = 2 * radius +let circumference = 2 * pi * radius +print(diameter) +print(circumference) //: page 1 of 8 | [Next: App Exercise - Fitness Calculations](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift index 7384367..d7af330 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift @@ -5,16 +5,29 @@ Your fitness tracker keeps track of users' heart rate, but you might also want to display their average heart rate over the last hour. Create three constants, `heartRate1`, `heartRate2`, and `heartRate3`. Give each constant a different value between 60 and 100. Create a constant `addedHR` equal to the sum of all three heart rates. Now create a constant called `averageHR` that equals `addedHR` divided by 3 to get the average. Print the result. */ - +let heartRate1 = 61 +let heartRate2 = 87 +let heartRate3 = 99 +let addedHR = heartRate1 + heartRate2 + heartRate3 +let averageHR = addedHR / 3 +print(averageHR) /*: Now create three more constants, `heartRate1D`, `heartRate2D`, and `heartRate3D`, equal to the same values as `heartRate1`, `heartRate2`, and `heartRate3`. These new constants should be of type `Double`. Create a constant `addedHRD` equal to the sum of all three heart rates. Create a constant called `averageHRD` that equals the `addedHRD` divided by 3 to get the average of your new heart rate constants. Print the result. Does this differ from your previous average? Why or why not? */ - +let heartRate1D: Double = 61 +let heartRate2D: Double = 87 +let heartRate3D: Double = 99 +let addedHRD = heartRate1D + heartRate2D + heartRate3D +let averageHRD = addedHRD / 3 +print(averageHRD) +// yes, because now its a Double and the decimal points will occure, while if we didn't specify the type it would be Int which means no decimal points, and now its more accurte. /*: Imagine that partway through the day a user has taken 3,467 steps out of the 10,000 step goal. Create constants `steps` and `goal`. Both will need to be of type `Double` so that you can perform accurate calculations. `steps` should be assigned the value 3,467, and `goal` should be assigned 10,000. Create a constant `percentOfGoal` that equals an expression that evaluates to the percent of the goal that has been achieved so far. */ - +let steps: Double = 3467 +let goal: Double = 10000 +let percentOfGoal = steps/goal * 100 //: [Previous](@previous) | page 2 of 8 | [Next: Exercise - Compound Assignment](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift index b0c97b5..7a76968 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift @@ -3,8 +3,12 @@ Declare a variable whose value begins at 10. Using addition, update the value to 15 using the compound assignment operator. Using multiplication, update the value to 30 using compound assignment. Print out the variable's value after each assignment. */ - - +var start = 10 +print(start) +start += 5 +print(start) +start *= 2 +print(start) /*: Create a variable called `piggyBank` that begins at 0. You will use this to keep track of money you earn and spend. For each point below, use the right compound assignment operator to update the balance in your piggy bank. @@ -16,9 +20,16 @@ Print the balance of your piggy bank after each step. */ - - - - - +var piggyBank = 0 +print(piggyBank) +piggyBank += 10 +print(piggyBank) +piggyBank += 20 +print(piggyBank) +piggyBank /= 2 +print(piggyBank) +piggyBank *= 3 +print(piggyBank) +piggyBank -= 3 +print(piggyBank) //: [Previous](@previous) | page 3 of 8 | [Next: App Exercise - Counting](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift index 881654a..fc0d2a2 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift @@ -5,13 +5,13 @@ The most basic feature of your fitness tracking app is counting steps. Create a variable `steps` and set it equal to 0. Then increment its value by 1 to simulate a user taking a step. */ - - +var steps = 0 +steps += 1 /*: In addition to tracking steps, your fitness tracking app tracks distance traveled. Create a variable `distance` of type `Double` and set it equal to 50. This will represent the user having traveled 50 feet. You decide, however, to display the distance in meters. 1 meter is approximately equal to 3 feet. Use a compound assignment operator to convert `distance` to meters. Print the result. */ - - +var distance: Double = 50 +distance /= 3 //: [Previous](@previous) | page 4 of 8 | [Next: Exercise - Order of Operations](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift index e8378a0..f9a8cd9 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift @@ -3,21 +3,21 @@ Print out what you think 10 + 2 * 5 evaluates to. Then print out the actual expression (i.e. `print(10 + 2 * 5)`) */ - - +print(20) +print(10+2*5) /*: In a separate `print` statement, add in the necessary parentheses so that addition takes place before multiplication. */ - +print((10+2) * 5) /*: Print out what you think 4 * 9 - 6 / 2 evaluates to. Then print out the actual expression. */ - - +print(33) +print(4*9 - 6/2) /*: In a separate `print` statement, add in the necessary parentheses so that the subtraction is prioritized over the multiplication and division. */ - +print(4 * (9-6) / 2) //: [Previous](@previous) | page 5 of 8 | [Next: App Exercise - Complex Fitness Calculations](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift index 1f2189c..43dfd51 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift @@ -5,13 +5,17 @@ If you completed the Fitness Calculations exercise, you calculated an average heart rate to display to the user. However, using proper order of operations you can do this in fewer steps. Create three separate heart rate constants, all of type `Double`, with values between 60 and 100. Then create a constant equal to the average heart rate. If you use correct order of operations you can do the heart calculation in one line. */ - +let heartRate1 = 61 +let heartRate2 = 87 +let heartRate3 = 99 +let averageHR = (heartRate1 + heartRate2 + heartRate3) / 3 +print(averageHR) /*: One feature you might want to give users is to display their current body temperature. Create a constant `tempInFahrenheit` equal to 98.6. You may want to also show the temperature in celsius. You can convert fahrenheit to celsius by taking `tempInFahrenheit` and subtracting 32, then multiplying the result by (5.0/9.0). Create a constant `tempInCelsius` that calculates in one line the temperature in celsius. */ - - +let tempInFahrenheit = 98.6 +let tempInCelsius = (tempInFahrenheit - 32) * (5.0/9.0) //: [Previous](@previous) | page 6 of 8 | [Next: Exercise - Numeric Type Conversion](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift index 93698b8..fb9db54 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift @@ -3,16 +3,18 @@ Create an integer constant `x` with a value of 10, and a double constant `y` with a value of 3.2. Create a constant `multipliedAsIntegers` equal to `x` times `y`. Does this compile? If not, fix it by converting your `Double` to an `Int` in the mathematical expression. Print the result. */ - - +let x: Int = 10 +let y: Double = 3.2 +let multipliedAsIntegers = x * Int(y) +print(multipliedAsIntegers) /*: Create a constant `multipliedAsDoubles` equal to `x` times `y`, but this time convert the `Int` to a `Double` in the expression. Print the result. */ - - +let multipliedAsDoubles = Double(x) * y +print(multipliedAsDoubles) /*: Are the values of `multipliedAsIntegers` and `multipliedAsDoubles` different? Print a statement to the console explaining why. */ - +print("having the values as Int made them lose some dicemal, but converting x to Double and keeping y as Double gave us an accurate result more than the one with Int") //: [Previous](@previous) | page 7 of 8 | [Next: App Exercise - Converting Types](@next) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift index 5de2f53..615ef11 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift @@ -7,8 +7,9 @@ Now create a constant `percentOfGoal` of type `Double` that equals the percent of the goal that has been reached so far. You'll need to convert your constants of type `Int` to be of type `Double` in your calculation. */ - - +let steps: Int = 4_677 +let goal: Int = 10_000 +let percentOfGoal: Double = Double(steps)/Double(goal) * 100 /*: _Copyright © 2018 Apple Inc._ diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift index e3db378..9ed1aca 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/1. Exercise - Logical Operators.xcplaygroundpage/Contents.swift @@ -1,55 +1,56 @@ /*: ## Exercise - Logical Operators - + For each of the logical expressions below, print out what you think the resulting value will be (`true` or `false`). Then print out the actual expression to see if you were right. An example has been provided below. - 43 == 53 - print(false) - print(43 == 53) - + 43 == 53 + print(false) + print(43 == 53) + 1. `9 == 9` */ - +print(true) +print(9 == 9) /*: 2. `9 != 9` */ - - +print(false) +print(9 != 9) /*: 3. `47 > 90` */ - - +print(false) +print( 47 > 90) /*: 4. `47 < 90` */ - - +print(true) +print(47 < 90) /*: 5. `4 <= 4` */ - - +print(true) +print(4 <= 4) /*: 6. `4 >= 5` */ - - +print(false) +print(4 >= 5) /*: 7. `(47 > 90) && (47 < 90)` */ - - +print(false) +print((47 > 90) && (47 < 90)) /*: 8. `(47 > 90) || (47 < 90)` */ - - +print(true) +print((47 > 90) || (47 < 90)) /*: 9. `!true` */ - - +print(false) +print(!true) //: page 1 of 9 | [Next: Exercise - If and If-Else Statements](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift index 5bb5325..24b7f4e 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift @@ -4,18 +4,28 @@ Imagine you're creating a machine that will count your money for you and tell you how wealthy you are based on how much money you have. A variable `dollars` has been given to you with a value of 0. Write an if statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0. Observe what is printed to the console. */ var dollars = 0 - - +if dollars == 0 { + print("Sorry, kid. You're broke!") +} /*: `dollars` has been updated below to have a value of 10. Write an an if-else statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0, but prints "You've got some spending money!" otherwise. Observe what is printed to the console. */ dollars = 10 - - +if dollars == 0 { + print("Sorry, kid. You're broke!") +} else { + print("You've got some spending money!") +} /*: `dollars` has been updated below to have a value of 105. Write an an if-else-if statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0, prints "You've got some spending money!" if `dollars` is less than 100, and prints "Looks to me like you're rich!" otherwise. Observe what is printed to the console. */ dollars = 105 - +if dollars == 0 { + print("Sorry, kid. You're broke!") +} else if dollars < 100 { + print("You've got some spending money!") +} else { + print("Looks to me like you're rich!") +} //: [Previous](@previous) | page 2 of 9 | [Next: App Exercise - Fitness Decisions](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift index d0d23b3..3b7184d 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift @@ -5,11 +5,22 @@ You want your fitness tracking app to give as much encouragement as possible to your users. Create a variable `steps` equal to the number of steps you guess you've taken today. Create a constant `stepGoal` equal to 10,000. Write an if-else statement that will print "You're almost halfway there!" if `steps` is less than half of `stepGoal`, and will print "You're over halfway there!" if `steps` is greater than half of `stepGoal`. */ - +var steps = 3457 +let stepGoal = 10_000 +if steps < stepGoal / 2 { + print("You're almost halfway there!") +} else { + print("You're over halfway there!") +} /*: Now create a new, but similar, if-else-if statement that prints "Way to get a good start today!" if `steps` is less than a tenth of `stepGoal`, prints "You're almost halfway there!" if `steps` is less than half of `stepGoal`, and prints "You're over halfway there!" if `steps` is greater than half of `stepGoal`. */ - - +if steps < stepGoal / 10 { + print("Way to get a good start today!") +} else if steps < stepGoal / 2 { + print("You're almost halfway there!") +} else { + print("You're over halfway there") +} //: [Previous](@previous) | page 3 of 9 | [Next: Exercise - Boolean Practice](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift index 5db9740..39f5928 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift @@ -12,7 +12,11 @@ let hasFish = true let hasPizza = false let hasVegan = true - +if hasVegan && (hasFish || hasPizza){ + print("Let's go!") +} else{ + print("Sorry, we'll have to think of somewhere else.") +} /*: Imagine you're trying to decide whether or not to go on a walk. You decide that you'll go on a walk if it's not raining or if it's 82 degress or warmer and sunny out. Create a constant `isNiceWeather` that is equal to an expression that evaluates to a boolean indicating whether or not the weather is nice enough for you to go for a walk. Write an if statement that will print "I'm going for a walk!" if the weather is nice. @@ -20,6 +24,8 @@ let hasVegan = true let temp = 82 let isRaining = true let isSunny = true - +if !isRaining || (temp >= 82 && isSunny) { + print("I'm going for a walk!") +} //: [Previous](@previous) | page 4 of 9 | [Next: App Exercise - Target Heart Rate](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift index 8740e75..69cf806 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift @@ -10,6 +10,12 @@ let targetLowerBound = 120 let targetUpperBound = 150 let currentHR = 147 - +if currentHR < 120 { + print("You're doing great, but try to push it a bit!") +} else if currentHR >= 120 && currentHR <= 150 { + print("You're right on track!") +} else { + print("You're on fire! Slow it down just a bit.") +} //: [Previous](@previous) | page 5 of 9 | [Next: Exercise - Switch Statements](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift index ed3e397..d12da4c 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift @@ -3,11 +3,26 @@ Imagine you're on a baseball team nearing the end of the season. Create a `leaguePosition` constant with a value of 1. Using a `switch` statement, print "Champions!" if the `leaguePosition` is 1, "Runners up" if the value is 2, "Third place" if the value is 3, and "Bad season!" in all other cases. */ - +let leaguePosition = 1 +switch leaguePosition { +case 1: + print("Champions!") +case 2: + print("Runners up") +case 3: + print("Third place") +default: + print("Bad season!") +} /*: Write a new `switch` statement that prints "Medal winner" if `leaguePosition` is within the range of 1-3. Otherwise, print "No medal awarded". */ - +switch leaguePosition { +case 1...3: + print("Medal winner") +default: + print("No medal awarded") +} //: [Previous](@previous) | page 6 of 9 | [Next: App Exercise - Heart Rate Zones](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift index 40f36d4..ee01607 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift @@ -16,6 +16,18 @@ If `currentHR` is above the listed zones, print some kind of warning asking the user to slow down. */ let currentHR = 128 - - +switch currentHR { +case 100...120: + print("You are in the Very Light zone. Activity in this zone helps with recovery.") +case 121...140: + print("You are in the Light zone. Activity in this zone helps improve basic endurance and fat burning.") +case 141...160: + print("You are in the Moderate zone. Activity in this zone helps improve aerobic fitness.") +case 161...180: + print("You are in the Hard zone. Activity in this zone increases maximum performance capacity for shorter sessions.") +case 181...200: + print("You are in the Maximum zone. Activity in this zone helps fit athletes develop speed.") +default: + print("You might want to slow down. This heart rate may not be safe.") +} //: [Previous](@previous) | page 7 of 9 | [Next: Exercise - Ternary Operator](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift index 24cbb56..e61619a 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift @@ -13,4 +13,5 @@ if number1 > number2 { largest = number2 } + largest = number1 > number2 ? number1 : number2 //: [Previous](@previous) | page 8 of 9 | [Next: App Exercise - Ternary Messages](@next) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift index 5c86dde..dc22d94 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift @@ -9,9 +9,9 @@ let stepGoal = 10000 let steps = 3948 if steps < stepGoal / 2 { - print("Almost halfway!") + print("Almost_halfway!") } else { - print("Over halfway!") + print("Over_halfway!") } diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.swift index d17ab77..cb83867 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.swift @@ -3,7 +3,7 @@ Create a `name` constant and assign it a string literal representing your name. */ - +let name = "Norah Mohsen" /*: Create a `favoriteQuote` constant and assign it the following string literal: @@ -15,12 +15,17 @@ - callout(Example): If your favorite quote is "The grass is always greener on the other side" the value of `favoriteQuote` should be such that printing `favoriteQuote` results in the following: * `My favorite quote is "The grass is always greener on the other side."` */ - +let favoriteQuote = "My favorite quote is \"My limit is beyond the sky \"" +print(favoriteQuote) /*: Write an if-else statement that prints "There's nothing here" if `emptyString` is empty, and "It's not as empty as I thought" otherwise. */ let emptyString = "" - +if emptyString.isEmpty { + print("There's nothing here") +} else { + print("It's not as empty as I thought") +} //: page 1 of 5 | [Next: Exercise - Concatenation and Interpolation](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.swift index 2bbe7f2..f833ddf 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.swift @@ -3,13 +3,17 @@ Create a `city` constant and assign it a string literal representing your home city. Then create a `state` constant and assign it a string literal representing your home state. Finally, create a `home` constant and use string concatenation to assign it a string representing your home city and state (i.e. Portland, Oregon). Print the value of `home`. */ - +let city = "Riyadh" +let state = "riyadh" +let home = city + ", " + state +print(home) /*: Use the compound assignment operator (`+=`) to add `home` to `introduction` below. Print the value of `introduction`. */ -var introduction = "I live in" - +var introduction = "I live in " +introduction += home +print(introduction) /*: Declare a `name` constant and assign it your name as a string literal. Then declare an `age` constant and give it your current age as an `Int`. Then print the following phrase using string interpolation: @@ -18,6 +22,8 @@ var introduction = "I live in" Insert `name` where indicated, and insert a mathematical expression that evaluates to your current age plus one where indicated. */ - +let name = "Norah Mohsen" +let age = 21 +print("My name is \(name) and after my next birthday I will be \(age + 1) years old") //: [Previous](@previous) | page 2 of 5 | [Next: App Exercise - Notifications](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.swift index abb8e54..61a93e2 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.swift @@ -7,7 +7,9 @@ Create `firstName` and `lastName` constants and assign them string literals representing a user's first name and last name, respectively. Create a `fullName` constant that uses string concatenation to combine `firstName` and `lastName`. Print the value of `fullName`. */ - +let firstName = "Norah" +let lastName = "Mohsen" +let fullName = firstName + " " + lastName /*: Occasionally users of your fitness tracking app will beat previous goals or records. You may want to notify them when this happens for encouragement purposes. Create a new constant `congratulations` and assign it a string literal that uses string interpolation to create the following string: @@ -18,6 +20,7 @@ */ let previousBest = 14392 let newBest = 15125 - +let congratulations = "Congratulations, \(fullName)! You beat your previous daily high score of \(previousBest) steps by walking \(newBest) steps yesterday!" +print(congratulations) //: [Previous](@previous) | page 3 of 5 | [Next: Exercise - String Equality and Comparison](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.swift index 3ee86db..7d1efec 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.swift @@ -3,7 +3,14 @@ Create two constants, `nameInCaps` and `name`. Assign `nameInCaps` your name as a string literal with proper capitalization. Assign `name` your name as a string literal in all lowercase. Write an if-else statement that checks to see if `nameInCaps` and `name` are the same. If they are, print "The two strings are equal", otherwise print "The two strings are not equal." */ +let nameInCaps = "Norah Mohsen" +let name = "norah mohsen" +if nameInCaps == name { + print("The two strings are equal") +} else { + print("The two strings are not equal.") +} /*: Write a new if-else statement that also checks to see if `nameInCaps` and `name` are the same. However, this time use the `lowercased()` method on each constant to compare the lowercase version of the strings. If they are equal, print the following statement using string interpolations: @@ -14,25 +21,33 @@ - " and are not the same." */ +if nameInCaps.lowercased() == name.lowercased() { + print("\(nameInCaps.lowercased()) and \(name.lowercased()) are the same") +} else { + print("\(nameInCaps.lowercased()) and \(name.lowercased()) are not the same") +} /*: Imagine you are looking through a list of names to find any that end in "Jr." Write an if statement below that will check if `junior` has the suffix "Jr.". If it does, print "We found a second generation name!" */ let junior = "Cal Ripken Jr." - - +if junior.hasSuffix("Jr.") { + print("We found a second generation name!") +} /*: Suppose you are trying to find a document on your computer that contains Hamlet's famous soliloquy written by Shakespeare. You write a simple app that will check every document to see if it contains the phrase "to be, or not to be". You decide to do part of this with the `contains(_:)` method. Write an if statement below that will check if `textToSearchThrough` contains `textToSearchFor`. If it does, print "I found it!" Be sure to make this functionality case insensitive. */ import Foundation let textToSearchThrough = "To be, or not to be--that is the question" let textToSearchFor = "to be, or not to be" - +if textToSearchThrough.lowercased().contains(textToSearchFor.lowercased()) { + print("I found it!") +} /*: Print to the console the number of characters in your name by using the `count` property on `name`. */ - +print(name.count) //: [Previous](@previous) | page 4 of 5 | [Next: App Exercise - Password Entry and User Search](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.swift index e396836..3436df8 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.swift @@ -9,7 +9,11 @@ let storedUserName = "TheFittest11" let storedPassword = "a8H1LuK91" let enteredUserName = "thefittest11" let enteredPassword: String = "a8H1Luk9" - +if enteredUserName.lowercased() == storedUserName.lowercased() && enteredPassword == storedPassword { + print("You are now logged in!") +} else { + print("Please check your user name and Password and try again.") +} /*: Now that users can log in, they need to be able to search through a list of users to find their friends. This might normally be done by having the user enter a name, and then looping through all user names to see if a user name contains the search term entered. You'll learn about loops later, so for now you'll just work through one cycle of that. Imagine you are searching for a friend whose user name is StepChallenger. You enter "step" into a search bar and the app begins to search. When the app comes to the user name "stepchallenger," it checks to see if "StepChallenger" contains "step." @@ -19,7 +23,9 @@ let enteredPassword: String = "a8H1Luk9" import Foundation let userName = "StepChallenger" let searchName = "step" - +if userName.lowercased().contains(searchName) { + print("StepChallenger") +} /*: diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.swift index 37d509b..59c1983 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.swift @@ -3,11 +3,33 @@ Write a function called `introduceMyself` that prints a brief introduction of yourself. Call the function and observe the printout. */ - +func introduceMyself(){ + print("Hi!! I'm Norah, I'm a software engineer ") +} /*: Write a function called `magicEightBall` that generates a random number and then uses either a switch statement or if-else-if statements to print different responses based on the random number generated. `let randomNum = Int.random(in: 0...4)` will generate a random number from 0 to 4, after which you can print different phrases corresponding to the number generated. Call the function multiple times and observe the different printouts. */ +func magicEightBall() { + let randomNum = Int.random(in: 0...4) + switch randomNum { + case 0: + print("I don't know what to say") + case 1: + print("you can do it!") + case 2: + print("HELLO!") + case 3: + print("Happy programming!") + default: + print("seems you're lucky") + } +} +magicEightBall() +magicEightBall() +magicEightBall() +magicEightBall() +magicEightBall() //: page 1 of 6 | [Next: App Exercise - A Functioning App](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.swift index 17808ef..7a22a69 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.swift @@ -8,12 +8,38 @@ A reoccurring process like this is a perfect candidate for a function. Write a function called `incrementSteps` after the declaration of `steps` below that will increment `steps` by one and then print its value. Call the function multiple times and observe the printouts. */ var steps = 0 +func incrementSteps(){ + steps += 1 + print(steps) +} +incrementSteps() +incrementSteps() +incrementSteps() +incrementSteps() +incrementSteps() +incrementSteps() + /*: Similarly, if you want to regularly provide progress updates to your user, you can put your control flow statements that check on progress into a function. Write a function called `progressUpdate` after the declaration of `goal` below. The function should print "You're off to a good start." if `steps` is less than 10% of `goal`, "You're almost halfway there!" if `steps` is less than half of `goal`, "You're over halfway there!" if `steps` is less than 90% of `goal`, "You're almost there!" if `steps` is less than `goal`, and "You beat your goal!" otherwise. Call the function and observe the printout. Remember, you can convert numbers using the appropriate Int or Double initializer. */ let goal = 10000 +let stepsPercentage = Double(steps/goal) +func progressUpdate(){ + if stepsPercentage < 0.1 { + print("You're off to a good start.") + } else if stepsPercentage < 0.5 { + print("You're almost halfway there!") + } else if stepsPercentage < 0.9 { + print("You're over halfway there!") + } else if steps < goal { + print("You're almost there!") + } else { + print("You beat your goal!") + } +} +progressUpdate() //: [Previous](@previous) | page 2 of 6 | [Next: Exercise - Parameters and Argument Labels](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.swift index 33158a5..c96745f 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.swift @@ -3,16 +3,30 @@ Write a new introduction function called `introduction`. It should take two `String` parameters, `name` and `home`, and one `Int` parameter, `age`. The function should print a brief introduction. I.e. if "Mary," "California," and 32 were passed into the function, it might print "Mary, 32, is from California." Call the function and observe the printout. */ - +func introduction(name:String, home :String,age :Int ){ + print("\(name), \(age), is from \(home).") + +} +introduction(name: "Norah", home: "Riyadh", age: 21) /*: Write a function called `almostAddition` that takes two `Int` arguments. The first argument should not require an argument label. The function should add the two arguments together, subtract 2, then print the result. Call the function and observe the printout. */ +func almostAddition(_ number1: Int, number2: Int) { + let result = number1 + number2 - 2 + print(result) +} +almostAddition(3, number2: 2) /*: Write a function called `multiply` that takes two `Double` arguments. The function should multiply the two arguments and print the result. The first argument should not require a label, and the second argument should have an external label, "by", that differs from the internal label. Call the function and observe the printout. */ +func multiply(_ number1: Int, by number2: Int) { + let result = number1 * number2 + print(result) +} +multiply(3, by: 2) //: [Previous](@previous) | page 3 of 6 | [Next: App Exercise - Progress Updates](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.swift index 29d57f3..9aac8bd 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.swift @@ -10,10 +10,35 @@ Call the function a number of times, passing in different values of `steps` and `goal`. Observe the printouts and make sure what is printed to the console is what you would expect for the parameters passsed in. */ +func progressUpdate(steps :Int, goal: Int ){ + + let stepsPercentage = Double(steps)/Double(goal) + if stepsPercentage < 0.1 { + print("You're off to a good start.") + } else if stepsPercentage < 0.5 { + print("You're almost halfway there!") + } else if stepsPercentage < 0.9 { + print("You're over halfway there!") + } else if steps < goal { + print("You're almost there!") + } else { + print("You beat your goal!") + } +} +progressUpdate(steps: 10000, goal: 21000) /*: Your fitness tracking app is going to help runners stay on pace to reach their goals. Write a function called pacing that takes four `Double` parameters called `currentDistance`, `totalDistance`, `currentTime`, and `goalTime`. Your function should calculate whether or not the user is on pace to hit or beat `goalTime`. If yes, print "Keep it up!", otherwise print "You've got to push it just a bit harder!" */ - +func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) { + let pace = currentTime/(currentDistance/totalDistance) + + if pace < goalTime { + print("Keep it up!") + } else { + print("You've got to push it just a bit harder!") + } +} +pacing(currentDistance: 60, totalDistance: 100, currentTime: 3.0, goalTime: 5.0) //: [Previous](@previous) | page 4 of 6 | [Next: Exercise - Return Values](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.swift index 2cdc442..4f8431b 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.swift @@ -3,11 +3,20 @@ Write a function called `greeting` that takes a `String` argument called name, and returns a `String` that greets the name that was passed into the function. I.e. if you pass in "Dan" the return value might be "Hi, Dan! How are you?" Use the function and print the result. */ +func greeting(name: String) -> String { + return "Hi, \(name)! How are you doing?" +} +print(greeting(name: "Norah")) /*: Write a function that takes two `Int` arguments, and returns an `Int`. The function should multiply the two arguments, add 2, then return the result. Use the function and print the result. */ +func almostMultiplication(number1: Int, number2: Int) -> Int { + return number1 * number2 + 2 +} +print(almostMultiplication(number1: 2, number2: 4)) + //: [Previous](@previous) | page 5 of 6 | [Next: App Exercise - Separating Functions](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.swift index 76dc16a..b77514f 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.swift @@ -7,12 +7,28 @@ As an example, write a function that only does a portion of what your previous `pacing` function did. This function will be called `calculatePace`. It should take three `Double` arguments called `currentDistance`, `totalDistance`, and `currentTime`, and should return a `Double` that will represent the time at which the user will finish the run based on the user's current distance and time. call the function and print the return value. */ +func calculatePace(currentDistance: Double, totalDistance: Double, currentTime: Double) -> Double { + return currentTime/(currentDistance/totalDistance) +} +let pace = calculatePace(currentDistance: 50, totalDistance: 100, currentTime: 4.8) +print(pace) /*: Now write a function called `pacing` that takes four `Double` arguments called `currentDistance`, `totalDistance`, `currentTime`, and `goalTime`. The function should also return a `String`, which will be the message to show the user. The function should call `calculatePace`, passing in the appropriate values, and capture the return value. The function should then compare the returned value to `goalTime` and if the user is on pace return "Keep it up!", and return "You've got to push it just a bit harder!" otherwise. Call the function and print the return value. */ +func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String { + let pace = calculatePace(currentDistance: currentDistance, totalDistance: totalDistance, currentTime: currentTime) + if pace <= goalTime { + return "Keep it up!" + } else { + return "You've got to push it just a bit harder!" + } +} + + +print(pacing(currentDistance: 34, totalDistance: 24, currentTime: 5.5, goalTime: 9)) /*: diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.swift index 61fd1f3..0eaed1b 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.swift @@ -3,26 +3,47 @@ Imagine you are creating an app that will monitor location. Create a `GPS` struct with two variable properties, `latitude` and `longitude`, both with default values of 0.0. */ +struct GPS { + var latitude = 0.0 + var longitude = 0.0 +} /*: Create a variable instance of `GPS` called `somePlace`. It should be initialized without supplying any arguments. Print out the latitude and longitude of `somePlace`, which should be 0.0 for both. */ - +var somePlace = GPS() +print("latitude is \(somePlace.latitude)") +print("longitude is \(somePlace.longitude)") /*: Change `somePlace`'s latitude to 51.514004, and the longitude to 0.125226, then print the updated values. */ - +somePlace.latitude = 51.514004 +somePlace.longitude = 0.125226 +print("latitude is \(somePlace.latitude)") +print("longitude is \(somePlace.longitude)") /*: Now imagine you are making a social app for sharing your favorite books. Create a `Book` struct with four variable properties: `title`, `author`, `pages`, and `price`. The default values for both `title` and `author` should be an empty string. `pages` should default to 0, and `price` should default to 0.0. */ - +struct Book { + var title = "" + var author = "" + var pages = 0 + var price = 0.0 +} /*: Create a variable instance of `Book` called `favoriteBook` without supplying any arguments. Print out the title of `favoriteBook`. Does it currently reflect the title of your favorite book? Probably not. Change all four properties of `favoriteBook` to reflect your favorite book. Then, using the properties of `favoriteBook`, print out facts about the book. */ - +var favoriteBook = Book() +print(favoriteBook.title) +favoriteBook.title = "Majdolin" +favoriteBook.author = "Mustafa Lutfi al-Manfaluti" +favoriteBook.pages = 233 +favoriteBook.price = 35 + +print("My favorite book is \(favoriteBook.title) written by \(favoriteBook.author). With \(favoriteBook.pages) pages, and it costs \(favoriteBook.price) Saudi Riyal.") //: page 1 of 10 | [Next: App Exercise - Workout Tracking](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/10. App Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/10. App Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift index 159bdcc..a599805 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/10. App Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/10. App Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift @@ -10,10 +10,21 @@ Call the method from outside of the struct and print the result to ensure that it works properly. */ struct RunningWorkout { + static var meterInFeet = 3.28084 + static var mileInMeters = 1600.0 + static func mileTimeFor(distance: Double, time: Double) -> Double { + return time/distance * 1600 + } var distance: Double var time: Double var elevation: Double } +print(RunningWorkout.mileTimeFor(distance: 3200, time: 720)) +/*: + It may be helpful to have a few type properties on `RunningWorkout` representing unit conversions (i.e. meters to mile, feet to meters, etc.). Go back and add a type property for `meterInFeet` and assign it 3.28084. Then add a type property for `mileInMeters` and assign it 1600.0. Print both of these values below. + */ +print(RunningWorkout.meterInFeet) +print(RunningWorkout.mileInMeters) /*: It may be helpful to have a few type properties on `RunningWorkout` representing unit conversions (i.e. meters to mile, feet to meters, etc.). Go back and add a type property for `meterInFeet` and assign it 3.28084. Then add a type property for `mileInMeters` and assign it 1600.0. Print both of these values below. */ diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.swift index 87c64af..842c23d 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.swift @@ -7,16 +7,27 @@ Create a `RunningWorkout` struct. It should have variables properties for `distance`, `time`, and `elevation`. All three properties should have default values of 0.0. */ - - +struct RunningWorkout { + var distance = 0.0 + var time = 0.0 + var elevation = 0.0 +} /*: Create a variable instance of `RunningWorkout` called `firstRun` without supplying any arguments. Print out all three properties of `firstRun`. This is a good example of when using default values is appropriate, seeing as all running workouts start with a distance, time, and elevation change of 0. */ +var firstRun = RunningWorkout() +print("distance is \(firstRun.distance)") +print("time is \(firstRun.time)") +print("elevation is \(firstRun.elevation)") /*: Now imagine that throughout the course of the run, you go a distance of 2,396 meters in 15.3 minutes, and gain 94 meters of elevation. Update the values of `firstRun`'s properties accordingly. Print a statement about your run using the values of each property. */ - - +firstRun.distance = 2396 +firstRun.time = 15.3 +firstRun.elevation = 94 +print("first run distance is \(firstRun.distance)") +print("-> time is \(firstRun.time)") +print("-> elevation is \(firstRun.elevation)") //: [Previous](@previous) | page 2 of 10 | [Next: Exercise - Memberwise and Custom Initializers](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.swift index 6afc9c6..a561c1a 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.swift @@ -3,17 +3,29 @@ If you completed the exercise Structs, Instances, and Default Values, you created a `GPS` struct with default values for properties of `latitude` and `longitude`. Create your `GPS` struct again, but this time do not provide default values. Both properties should be of type `Double`. */ - +struct GPS { + var latitude : Double + var longitude : Double +} /*: Now create a constant instance of `GPS` called `somePlace`, and use the memberwise initializer to set `latitude` to 51.514004, and `longitude` to 0.125226. Print the values of `somePlace`'s properties. */ - - +let somePlace = GPS(latitude: 51.514004, longitude: 0.125226) +print("latitude is \(somePlace.latitude)") +print("longitude is \(somePlace.longitude)") /*: In Structs, Instance, and Default Values, you also created a `Book` struct with properties `title`, `author`, `pages`, and `price`. Create this struct again without default values. Give each property the appropriate type. Declare your `favoriteBook` instance and pass in the values of your favorite book using the memberwise initializer. Print a statement about your favorite book using `favoriteBook`'s properties. */ +struct Book { + var title: String + var author: String + var pages: Int + var price: Double +} +var favoriteBook = Book(title: "Majdolin", author: "Mustafa Lutfi al-Manfaluti", pages: 233, price: 35) +print("My favorite book is \(favoriteBook.title) written by \(favoriteBook.author). With \(favoriteBook.pages) pages, and it costs \(favoriteBook.price) Saudi Riyal.") /*: Make a `Height` struct with two variable properties, `heightInInches` and `heightInCentimeters`. Both should be of type `Double`. @@ -22,16 +34,30 @@ - Example: If you use the initializer for inches to pass in a height of 65, the initializer should set `heightInInches` to 65 and `heightInCentimeters` to 165.1. */ - +struct Height { + var heightInInches: Double + var heightInCentimeters: Double + + init(heightInInches: Double) { + self.heightInInches = heightInInches + self.heightInCentimeters = heightInInches*2.54 + } + + init(heightInCentimeters: Double) { + self.heightInCentimeters = heightInCentimeters + self.heightInInches = heightInCentimeters/2.54 + } +} /*: Now create a variable instance of `Height` called `someonesHeight`. Use the initializer for inches to set the height to 65. Print out the property for height in centimeters and verify that it is equal to 165.1. */ - +var someonesHeight = Height(heightInInches: 65) +print("height In Centimeters \(someonesHeight.heightInCentimeters)") /*: Now create a variable instance of `Height` called `myHeight` and initialize it with your own height. Verify that both `heightInInches` and `heightInCentimeters` are accurate. */ - - +var myHeight = Height(heightInCentimeters: 165.1) +print("height In Inches \(myHeight.heightInInches)") //: [Previous](@previous) | page 3 of 10 | [Next: App Exercise - Users and Distance](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.swift index 751d413..4783975 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.swift @@ -5,28 +5,49 @@ For most apps you'll need to have a data structure to hold information about a user. Create a `User` struct that has properties for basic information about a user. At a minimum, it should have properties to represent a user's name, age, height, weight, and activity level. You could do this by having `name` be a `String`, `age` be an `Int`, `height` and `weight` be of type `Double`, and `activityLevel` be an `Int` that will represent a scoring 1-10 of how active they are. Implement this now. */ - +struct User { + var name: String + var age: Int + var height: Double + var weight: Double + var activityLevel: Int +} /*: Create a variable instance of `User` and call it your name. Use the memberwise initializer to pass in information about yourself. Then print out a description of your `User` instance using the instance's properties. */ - +var userNorah = User(name: "Norah", age: 21, height: 155, weight: 42, activityLevel: 7) +print("My name is \(userNorah.name). I am \(userNorah.age) years old, my weight is \(userNorah.weight) kg and my height is \(userNorah.height) cm. On an activity scale from 1-10 I am probably a \(userNorah.activityLevel).") /*: In previous app exercises, you've worked with distance in the fitness tracking app example as a simple number. However, distance can be represented using a variety of units of measurement. Create a `Distance` struct that will represent distance in various units of measurement. At a minimum, it should have a `meters` property and a `feet` property. Create a custom initializer corresponding to each property (i.e. if you only have the two properties for meters and feet you will then have two initializers) that will take in a distance in one unit of measurement and assign the correct value to both units of measurements. Hint: *1 meter = 3.28084 feet*. - Example: If you use the initializer for meters and pass in a distance of 1600, the initializer should set `meters` to 1600 and `feet` to 5249.344. */ - +struct Distance { + var meters: Double + var feet: Double + + init(meters: Double) { + self.meters = meters + self.feet = meters*3.28084 + } + init(feet: Double) { + self.feet = feet + self.meters = feet/3.28084 + } +} /*: Now create an instance of `Distance` called `mile`. Use the initializer for meters to set the distance to 1600. Print out the property for feet and verify that it is equal to 5249.344. */ - +let mile = Distance(meters: 1600) +print(mile.feet) /*: Now create another instance of `Distance` and give it some other distance. Ensure that both properties are set correctly. */ - +let someDistance = Distance(meters: 2000) +print(someDistance.feet) //: [Previous](@previous) | page 4 of 10 | [Next: Exercise - Methods](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.swift index d90b425..bc51c67 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.swift @@ -8,7 +8,13 @@ struct Book { var author: String var pages: Int var price: Double + func description() { + print(" Title: \(title)\n Written by: \(author)\n Pages total: \(pages)\n Cost \(price) SAR.") + } } +let favoriteBook = Book(title: "Majdolin", author: "Mustafa Lutfi al-Manfaluti", pages: 233, price: 35) +favoriteBook.description() + /*: A `Post` struct has been created for you below, representing a generic social media post. Add a mutating method on `Post` called `like` that will increment `likes` by one. Then create an instance of `Post` and call `like()` on it. Print out the `likes` property before and after calling the method to see whether or not the value was incremented. */ @@ -16,5 +22,12 @@ struct Post { var message: String var likes: Int var numberOfComments: Int + mutating func like() { + likes += 1 + } } +var aPost = Post(message: "How was your day?", likes: 2, numberOfComments: 3) +print(aPost.likes) +aPost.like() +print(aPost.likes) //: [Previous](@previous) | page 5 of 10 | [Next: App Exercise - Workout Functions](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.swift index 774deca..b96b757 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.swift @@ -9,12 +9,25 @@ struct RunningWorkout { var distance: Double var time: Double var elevation: Double + func postWorkoutStats() { + print("Distance is \(distance)\nTime is \(time)\nElevation is \(elevation)") + } } +let run = RunningWorkout(distance: 1890, time: 400, elevation: 14) +run.postWorkoutStats() /*: A `Steps` struct has been created for you below, representing the day's step-tracking data. It has the goal number of steps for the day and the number of steps taken so far. Create a method on `Steps` called `takeStep` that increments the value of `steps` by one. Then create an instance of `Steps` and call `takeStep()`. Print the value of the instance's `steps` property before and after the method call. */ struct Steps { var steps: Int var goal: Int + mutating func takeStep() { + steps += 1 + } } + +var steps = Steps(steps: 567, goal: 5000) +print(steps.steps) +steps.takeStep() +print(steps.steps) //: [Previous](@previous) | page 6 of 10 | [Next: Exercise - Computed Properties and Property Observers](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.swift index cb8e9f0..2c75b9e 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.swift @@ -6,17 +6,35 @@ struct Rectangle { var width: Int var height: Int + var area: Int { + return width*height + } } +var rectangle = Rectangle(width: 3, height: 8) +print(rectangle.area) /*: In the `Height` struct below, height is represented in both inches and centimeters. However, if `heightInInches` is changed, `heightInCentimeters` should also adjust to match it. Add a `didSet` to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property even though it already has the right value, you will end up with an infinite loop of each property setting the other. Create an instance of `Height` and then change one of its properties. Print out the other property to ensure that it was adjusted accordingly. */ struct Height { - var heightInInches: Double - - var heightInCentimeters: Double + var heightInInches: Double { + didSet { + let inCm = heightInInches*2.54 + if heightInCentimeters != inCm { + heightInCentimeters = inCm + } + } + } + var heightInCentimeters: Double { + didSet { + let inInches = heightInCentimeters/2.54 + if heightInInches != inInches { + heightInInches = inInches + } + } + } init(heightInInches: Double) { self.heightInInches = heightInInches self.heightInCentimeters = heightInInches*2.54 @@ -27,4 +45,7 @@ struct Height { self.heightInInches = heightInCentimeters/2.54 } } +var height = Height(heightInInches: 30) +height.heightInInches = 30 +print(height.heightInCentimeters) //: [Previous](@previous) | page 7 of 10 | [Next: App Exercise - Mile Times and Congratulations](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.swift index c53c59b..a4b9621 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.swift @@ -11,18 +11,31 @@ struct RunningWorkout { var distance: Double var time: Double var elevation: Double + var averageMileTime: Double { + return time/distance * 1600 + } } + +print(RunningWorkout(distance: 4000, time: 1100, elevation: 11).averageMileTime) /*: In other app exercises, you've provided encouraging messages to the user based on how many steps they've completed. A great place to check whether or not you should display something to the user is in a property observer. In the `Steps` struct below, add a `willSet` to the `steps` property that will check if the new value is equal to `goal`, and if it is, prints a congratulatory message. Create an instance of `Steps` where `steps` is 9999 and `goal` is 10000, then call `takeStep()` and see if your message is printed to the console. */ struct Steps { - var steps: Int + var steps: Int { + willSet { + if newValue == goal { + print("Congratulations! You did it! You met your goal for today!") + } + } + } var goal: Int mutating func takeStep() { steps += 1 } } +var steps = Steps(steps: 3999, goal: 4000) +steps.takeStep() //: [Previous](@previous) | page 8 of 10 | [Next: Exercise - Type Properties and Methods](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift index b4c4457..87ce2a8 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift @@ -7,12 +7,22 @@ struct User { var userName: String var email: String var age: Int + + static var currentUser: User = User(userName: "Norah", email: "Norah@example.com", age: 21) + static func logIn(user: User) { + currentUser = user + print("\(currentUser.userName) logged in succsfuly.") + } + } /*: There are other properties and actions associated with a `User` struct that might be good candidates for a type property or method. One might be a method for logging in. Go back and create a type method called `logIn(user:)` where `user` is of type `User`. In the body of the method, assign the passed in user to the `currentUser` property, and print out a statement using the user's userName saying that the user has logged in. Below, call the `logIn(user:)` method and pass in a different `User` instance than what you assigned to currentUser above. Observe the printout in the console. */ +let aUser = User(userName: "Noura", email: "Noura@friend.com", age: 20) +User.logIn(user: aUser) +//User.logIn(user: User.currentUser) //: [Previous](@previous) | page 9 of 10 | [Next: App Exercise - Type Properties and Methods](@next) diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.swift index 8576732..f35f26c 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.swift @@ -5,21 +5,48 @@ Create a `Spaceship` class with three variable properties: `name`, `health`, and `position`. The default value of `name` should be an empty string and `health` should be 0. `position` will be represented by an `Int` where negative numbers place the ship further to the left and positive numbers place the ship further to the right. The default value of `position` should be 0. */ - +class Spaceship{ + var name = "" + var health = 100 + var position = 0 + + func moveLeft() { + position -= 1 + } + + func moveRight() { + position += 1 + } + + func wasHit() { + health -= 5 + if health <= 0 { + print("Sorry, your ship was hit one too many times. Do you want to play again?") + } + } +} /*: Create a `let` constant called `falcon` and assign it to an instance of `Spaceship`. After initialization, set `name` to "Falcon". */ - +let falcon = Spaceship() +falcon.name = "Falcon" /*: Go back and add a method called `moveLeft()` to the definition of `Spaceship`. This method should adjust the position of the spaceship to the left by one. Add a similar method called `moveRight()` that moves the spaceship to the right. Once these methods exist, use them to move `falcon` to the left twice and to the right once. Print the new position of `falcon` after each change in position. */ - +print(falcon.position) +falcon.moveLeft() +print(falcon.position) +falcon.moveLeft() +print(falcon.position) +falcon.moveRight() +print(falcon.position) /*: The last thing `Spaceship` needs for this example is a method to handle what happens if the ship gets hit. Go back and add a method `wasHit()` to `Spaceship` that will decrement the ship's health by 5, then if `health` is less than or equal to 0 will print "Sorry. Your ship was hit one too many times. Do you want to play again?" Once this method exists, call it on `falcon` and print out the value of `health`. */ - +falcon.wasHit() +print(falcon.health) //: page 1 of 4 | [Next: Exercise - Create a Subclass](@next) diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.swift index 69a1117..84a0edd 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.swift @@ -26,21 +26,45 @@ class Spaceship { /*: Define a new class `Fighter` that inherits from `Spaceship`. Add a variable property `weapon` that defaults to an empty string and a variable property `remainingFirePower` that defaults to 5. */ - +class Fighter: Spaceship { + var weapon = "" + var remainingFirePower = 5 + + func fire() { + if remainingFirePower > 0 { + remainingFirePower -= 1 + } else { + print("You have no more fire power.") + } + } +} /*: Create a new instance of `Fighter` called `destroyer`. A `Fighter` will be able to shoot incoming objects to avoid colliding with them. After initialization, set `weapon` to "Laser" and `remainingFirePower` to 10. Note that since `Fighter` inherits from `Spaceship`, it also has properties for `name`, `health`, and `position`, and has methods for `moveLeft()`, `moveRight()`, and `wasHit()` even though you did not specifically add them to the declaration of `Fighter`. Knowing that, set `name` to "Destroyer," print `position`, then call `moveRight()` and print `position` again. */ - +let destroyer = Fighter() +destroyer.weapon = "Laser" +destroyer.remainingFirePower = 10 +destroyer.name = "Destroyer" +print(destroyer.position) +destroyer.moveRight() +print(destroyer.position) /*: Try to print `weapon` on `falcon`. Why doesn't this work? Provide your answer in a comment or a print statement below, and remove any code you added that doesn't compile. */ - +//let falcon = Spaceship() +//print(falcon.weapon) +print("falcon is a Spaceship but it doesn't have Fighter properties, because it doesn't inhert it, if it was Fighter we can call the weapon ") /*: Add a method to `fighter` called `fire()`. This should check to see if `remainingFirePower` is greater than 0, and if so, should decrement `remainingFirePower` by one. If `remainingFirePower` is not greater than 0, print "You have no more fire power." Call `fire()` on `destroyer` a few times and print `remainingFirePower` after each method call. */ - +destroyer.fire() +print(destroyer.remainingFirePower) +destroyer.fire() +print(destroyer.remainingFirePower) +destroyer.fire() +print(destroyer.remainingFirePower) //: [Previous](@previous) | page 2 of 4 | [Next: Exercise - Override Methods and Properties](@next) diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.swift index a25449d..c9b5884 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.swift @@ -39,16 +39,38 @@ class Fighter: Spaceship { /*: Define a new class `ShieldedShip` that inherits from `Fighter`. Add a variable property `shieldStrength` that defaults to 25. Create a new instance of `ShieldedShip` called `defender`. Set `name` to "Defender" and `weapon` to "Cannon." Call `moveRight()` and print `position`, then call `fire()` and print `remainingFirePower`. */ +class ShieldedShip: Fighter { + var shieldStrength = 25 + + override func wasHit() { + if shieldStrength > 0 { + shieldStrength -= 5 + } else { + super.wasHit() + } + } +} +let defender = ShieldedShip() +defender.name = "Defender" +defender.weapon = "Cannon" +defender.moveRight() +print(defender.position) +defender.fire() +print(defender.remainingFirePower) /*: Go back to your declaration of `ShieldedShip` and override `wasHit()`. In the body of the method, check to see if `shieldStrength` is greater than 0. If it is, decrement `shieldStrength` by 5. Otherwise, decrement `health` by 5. Call `wasHit()` on `defender` and print `shieldStrength` and `health`. */ - +defender.wasHit() +print(defender.shieldStrength) +print(defender.health) /*: When `shieldStrength` is 0, all `wasHit()` does is decrement `health` by 5. That's exactly what the implementation of `wasHit()` on `Spaceship` does! Instead of rewriting that, you can call through to the superclass implementation of `wasHit()`. Go back to your implementation of `wasHit()` on `ShieldedShip` and remove the code where you decrement `health` by 5 and replace it with a call to the superclass' implementation of the method. Call `wasHit()` on `defender`, then print `shieldStrength` and `health`. */ - +defender.wasHit() +print(defender.shieldStrength) +print(defender.health) //: [Previous](@previous) | page 3 of 4 | [Next: Exercise - Class Memberwise Initializers and References](@next) diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.swift index 5a9f7fa..07da734 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.swift @@ -7,7 +7,11 @@ class Spaceship { let name: String var health: Int var position: Int - + init(name: String, health: Int, position: Int) { + self.name = name + self.health = health + self.position = position + } func moveLeft() { position -= 1 } @@ -27,7 +31,11 @@ class Spaceship { class Fighter: Spaceship { let weapon: String var remainingFirePower: Int - + init(weapon: String, remainingFirePower: Int, name: String, health: Int, position: Int) { + self.weapon = weapon + self.remainingFirePower = remainingFirePower + super.init(name: name, health: health, position: position) + } func fire() { if remainingFirePower > 0 { remainingFirePower -= 1 @@ -39,7 +47,11 @@ class Fighter: Spaceship { class ShieldedShip: Fighter { var shieldStrength: Int - + init(shieldStrength: Int, weapon: String, remainingFirePower: Int, name: String, health: Int, position: Int) { + self.shieldStrength = shieldStrength + super.init(weapon: weapon, remainingFirePower: remainingFirePower, name: name, health: health, position: position) + } + override func wasHit() { if shieldStrength > 0 { shieldStrength -= 5 @@ -53,25 +65,34 @@ class ShieldedShip: Fighter { Then create an instance of `Spaceship` below called `falcon`. Use the memberwise initializer you just created. The ship's name should be "Falcon." */ - +let falcon = Spaceship(name: "Falcon", health: 100, position: 0) /*: Writing initializers for subclasses can get tricky. Your initializer needs to not only set the properties declared on the subclass, but also set all of the uninitialized properties on classes that it inherits from. Go to the declaration of `Fighter` and write an initializer that takes an argument for each property on `Fighter` and for each property on `Spaceship`. Set the properties accordingly. (Hint: you can call through to a superclass' initializer with `super.init` *after* you initialize all of the properties on the subclass). Then create an instance of `Fighter` below called `destroyer`. Use the memberwise initializer you just created. The ship's name should be "Destroyer." */ - +let destroyer = Fighter(weapon: "Laser", remainingFirePower: 10, name: "Destroyer", health: 75, position: 0) /*: Now go add an initializer to `ShieldedShip` that takes an argument for each property on `ShieldedShip`, `Fighter`, and `Spaceship`, and sets the properties accordingly. Remember that you can call through to the initializer on `Fighter` using `super.init`. Then create an instance of `ShieldedShip` below called `defender`. Use the memberwise initializer you just created. The ship's name should be "Defender." */ +let defender = ShieldedShip(shieldStrength: 18, weapon: "Cannon", remainingFirePower: 8, name: "Defender", health: 65, position: 0) /*: Create a new instance of `Spaceship` called `sameShip` and set it equal to `falcon`. Print out the position of `sameShip` and `falcon`, then call `moveLeft()` on `sameShip` and print out the position of `sameShip` and `falcon` again. Did both positions change? Why? If both were structs instead of classes, would it be the same? Why or why not? Provide your answer in a comment or print statement below. */ +let sameShip = falcon +print(falcon.position) +print(sameShip.position) +sameShip.moveLeft() +print(falcon.position) +print(sameShip.position) +print("sameShip and falcon are referencing the same instance in memory, if you change one of them the other will reflect the change on it, bthey are the same thing, but if it was a struct it would be totally diffrent and they will be seperate from each other") + /*: diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.swift index fbcf943..cb9ae8c 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.swift @@ -3,31 +3,36 @@ Assume you are an event coordinator for a community charity event and are keeping a list of who has registered. Create a variable `registrationList` that will hold strings. It should be empty after initialization. */ - +var registrationList: [String] = [] /*: Your friend Sara is the first to register for the event. Add her name to `registrationList` using the `append(_:)` method. Print the contents of the collection. */ - +registrationList.append("Sarah") +print(registrationList) /*: Add four additional names into the array using the `+=` operator. All of the names should be added in one step. Print the contents of the collection. */ - +registrationList += ["Noura", "Riham", "Najla", "Atheer"] +print(registrationList) /*: Use the `insert(_:at:)` method to add `Charlie` into the array as the second element. Print the contents of the collection. */ - +registrationList.insert("Charlie", at: 1) +print(registrationList) /*: Someone had a conflict and decided to transfer her registration to someone else. Use array subscripting to change the sixth element to `Rebecca`. Print the contents of the collection. */ - +registrationList[5] = "Rebecca" +print(registrationList) /*: Call `removeLast()` on `registrationList`. If done correctly, this should remove `Rebecca` from the collection. Store the result of `removeLast()` into a new constant `deletedItem`, then print `deletedItem`. */ - +let deletedItem = registrationList.removeLast() +print(deletedItem) //: page 1 of 4 | [Next: App Exercise - Activity Challenge](@next) diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.swift index d9e7443..5c62c09 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.swift @@ -7,26 +7,37 @@ Using arrays of type `String`, create at least two lists, one for walking challenges, and one for running challenges. Each should have at least two challenges and should be initialized using an array literal. Feel free to create more lists for different activities. */ +var walkingChallenges: [String] = ["Walk 3 miles a day", "Walk 5 miles a day"] +var runningChallenges: [String] = ["Run 5 times a week", "Run 7 times a week"] +var weightliftingChallenges: [String] = ["Load a bar up with your body weight. Put it on your shoulders, do 50 reps"] /*: In your app you want to show all of these lists on the same screen grouped into sections. Create a `challenges` array that holds each of the lists you have created (it will be an array of arrays). Using `challenges`, print the first element in the second challenge list. */ - +var challenges = [walkingChallenges, runningChallenges,weightliftingChallenges] +print(challenges[1][0]) /*: All of the challenges will reset at the end of the month. Use the `removeAll` to remove everything from `challenges`. Print `challenges`. */ - +challenges.removeAll() +print(challenges) /*: Create a new array of type `String` that will represent challenges a user has committed to instead of available challenges. It can be an empty array or have a few items in it. */ - +var committedChallenges: [String] = [] /*: Write an if statement that will use `isEmpty` to check if there is anything in the array. If there is not, print a statement asking the user to commit to a challenge. Add an else-if statement that will print "The challenge you have chosen is " if the array count is exactly 1. Then add an else statement that will print "You have chosen multiple challenges." */ - +if committedChallenges.isEmpty { + print("please go to commit a challenge") +} else if committedChallenges.count == 1 { + print("The challenge you have chosen is \(committedChallenges[0])") +} else { + print("You have chosen multiple challenges.") +} //: [Previous](@previous) | page 2 of 4 | [Next: Exercise - Dictionaries](@next) diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.swift index e3d52d8..9a69550 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.swift @@ -3,22 +3,25 @@ Create a variable `[String: Int]` dictionary that can be used to look up the number of days in a particular month. Use a dictionary literal to initialize it with January, February, and March. January contains 31 days, February has 28, and March has 31. Print the dictionary. */ - - +var lookUpNumDaysInMonths: [String: Int] = ["January": 31, "February": 28, "March": 31] +print(lookUpNumDaysInMonths) /*: Using subscripting syntax to add April to the collection with a value of 30. Print the dictionary. */ - +lookUpNumDaysInMonths["April"] = 30 +print(lookUpNumDaysInMonths) /*: It's a leap year! Update the number of days in February to 29 using the `updateValue(_:, forKey:)` method. Print the dictionary. */ - - +lookUpNumDaysInMonths.updateValue(29, forKey: "February") +print(lookUpNumDaysInMonths) /*: Use if-let syntax to retrieve the number of days under "January". If the value is there, print "January has 31 days", where 31 is the value retrieved from the dictionary. */ - +if let daysofJan = lookUpNumDaysInMonths["January"] { + print("January has \(daysofJan) days.") +} /*: Given the following arrays, create a new [String : [String]] dictionary. `shapesArray` should use the key "Shapes" and `colorsArray` should use the key "Colors". Print the resulting dictionary. @@ -26,10 +29,13 @@ let shapesArray = ["Circle", "Square", "Triangle"] let colorsArray = ["Red", "Green", "Blue"] - +let shapesColors: [String: [String]] = ["Shapes": shapesArray, "Colors": colorsArray] +print(shapesColors) /*: Print the last element of `colorsArray`, accessing it through the dictionary you've created. You'll have to use if-let syntax or the force unwrap operator to unwrap what is returned from the dictionary before you can access an element of the array. */ - +if let colors = shapesColors["Colors"] { + print(colors[2]) +} //: [Previous](@previous) | page 3 of 4 | [Next: App Exercise - Pacing](@next) diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.swift index 6b5ba08..0c7e58f 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.swift @@ -7,26 +7,34 @@ Create a dictionary `paces` of type [String: Double] and assign it a dictionary literal with "Easy", "Medium", and "Fast" keys corresponding to values of 10.0, 8.0, and 6.0. These numbers correspond to mile pace in minutes. Print the dictionary. */ - +var paces: [String: Double] = ["Easy": 10.0, "Medium": 8.0, "Fast": 6.0] +print(paces) /*: Add a new key/value pair to the dictionary. The key should be "Sprint" and the value should be 4.0. Print the dictionary. */ - +paces["Sprint"] = 4.0 +print(paces) /*: Imagine the user in question gets faster over time and decides to update his/her pacing on runs. Update the values of "Medium" and "Fast" to 7.5 and 5.8, respectively. Print the dictionary. */ - +paces.updateValue(7.5, forKey: "Medium") +paces["Fast"] = 5.8 +print(paces) /*: Imagine the user in question decides not to store "Sprint" as one his/her regular paces. Remove "Sprint" from the dictionary. Print the dictionary. */ - +paces.removeValue(forKey: "Sprint") +print(paces) /*: When a user chooses a pace, you want the app to print a statement stating that it will keep him/her on pace. Imagine a user chooses "Medium." Accessing the value from the dictionary, print a statement saying "Okay! I'll keep you at a minute mile pace." */ +if let pace = paces["Medium"] { + print("Okay! I'll keep you at a \(pace) minute mile pace.") +} /*: diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift index a019d00..f5a1657 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift @@ -3,17 +3,26 @@ Create a for-in loop that loops through values 1 to 100, and prints each of the values. */ - +for i in 1...100 { + print(i) +} /*: Create a for-in loop that loops through each of the characters in the `alphabet` string below, and prints each of the values alongside the index. */ let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +for (index, letter) in alphabet.enumerated() { + print("index: \(index), letter: \(letter)") +} /*: Create a `[String: String]` dictionary, where the keys are names of states and the values are their capitals. Include at least three key/value pairs in your collection, then use a for-in loop to iterate over the pairs and print out the keys and values in a sentence. */ +let statesCapitals: [String: String] = ["Alabama": "Montgomery", "Alaska": "Juneau", "Arizona": "Phoenix"] +for (state, capital) in statesCapitals { + print("State: \(state), Capital: \(capital)") +} //: page 1 of 6 | [Next: App Exercise - Movements](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.swift index 20d0853..025f9a4 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.swift @@ -6,12 +6,16 @@ Suppose your app contains a list of different movements that can be tracked. You want to display each item in the list to the user. Use a for-in loop to loop through `movements` below and print each movement. */ let movements: [String] = ["Walking", "Running", "Swimming", "Cycling", "Skiing", "Climbing"] - +for movement in movements { + print(movement) +} /*: Now suppose your app uses a dictionary to keep track of your average heart rate during each of the movements in `movements`. The keys correspond to the movements listed above, and the values correspond to the average heart rate that your fitness tracker has monitored during the given movement. Loop through `movementHeartRates` below, printing statements telling the user his/her average heart rate during each exercise. */ var movementHeartRates: [String: Int] = ["Walking": 85, "Running": 120, "Swimming": 130, "Cycling": 128, "Skiing": 114, "Climbing": 129] - +for (movement, heartRate) in movementHeartRates { + print("Movement: \(movement), average heart rate: \(heartRate).") +} //: [Previous](@previous) | page 2 of 6 | [Next: Exercise - While Loops](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.swift index e03b910..2904ff0 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.swift @@ -4,6 +4,11 @@ import Foundation Create a while loop that simulates rolling a 6-sided dice repeatedly until a 1 is rolled. After each roll, print the value. (Hint: use `Int.random(in: 1...6)` to generate a random number between 1 and 6). */ +var roll = 0 +while roll != 1 { + roll = Int.random(in: 1...6) + print(roll) +} //: [Previous](@previous) | page 3 of 6 | [Next: App Exercise - While Loops](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.swift index 120197b..f30639d 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.swift @@ -10,12 +10,19 @@ import Foundation */ let cadence: Double = 180 var testSteps = 0 - - +while testSteps < 10 { + print("Take a step") + testSteps += 1 + Thread.sleep(forTimeInterval: 60/cadence) +} /*: Recreate the above cadence example using a repeat-while loop. */ testSteps = 0 - +repeat { + print("Take a step") + testSteps += 1 + Thread.sleep(forTimeInterval: 60/cadence) +} while testSteps < 10 //: [Previous](@previous) | page 4 of 6 | [Next: Exercise - Control Transfer Statements](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.swift index bc9d7c4..2b5a830 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.swift @@ -4,11 +4,23 @@ Create a for-in loop that will loop through `alphabet`. Inside the loop, print every other letter by continuing to the next iteration if you are on a letter you do not wish to print. (Hint: You can use the modulo operator to only print even indexed characters). */ let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - - +for (index, letter) in alphabet.enumerated() { + if (index % 2 != 0) { + continue + } + print("\(index): \(letter)") +} /*: Create a `[String: String]` dictionary where the keys are names of states and the values are their capitals. Include at least three key/value pairs in your collection, with one of them being your home state. Now loop through this dictionary again, printing out the keys and values in a sentence, but add an if statement that will check if the current iteration is your home state. If it is, print("I found my home!") and break out of the loop. */ - +let statesCapitals: [String: String] = ["Alabama": "Montgomery", "Alaska": "Juneau", "Arizona": "Phoenix"] +for (state, capital) in statesCapitals { + if state == "Alabama" { + print("I found my home!!") + print(state) + break + } + print("State: \(state), Capital \(capital)") +} //: [Previous](@previous) | page 5 of 6 | [Next: App Exercise - Finding Movements](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.swift index 1649e6d..cc6a6b6 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.swift @@ -12,7 +12,13 @@ let lowHR = 110 let highHR = 125 var movementHeartRates: [String: Int] = ["Walking": 85, "Running": 120, "Swimming": 130, "Cycling": 128, "Skiing": 114, "Climbing": 129] +for (movement, heartRate) in movementHeartRates { + if heartRate < lowHR || heartRate > highHR { + continue + } + print("You could go \(movement).") +} /*: _Copyright © 2018 Apple Inc._ diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.swift index 6a8462d..958d3ab 100644 --- a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.swift @@ -7,23 +7,29 @@ Declare a constant `userInputAge` of type `String` and assign it "34e" to simulate a typo while typing age. Then declare a constant `userAge` of type `Int` and set its value using the `Int` initializer that takes an instance of `String` as input. Pass in `userInputAge` as the argument for the initializer. What error do you get? */ +let userInputAge = "34" +let userAge: Int? = Int(userInputAge) +//let userInputAge = "34e" >> prints nil - +print(userAge) /*: Go back and change the type of `userAge` to `Int?`, and print the value of `userAge`. Why is `userAge`'s value `nil`? Provide your answer in a comment or print statement below. */ - +print(userAge) +//userAge is nil because 34e is not Int /*: Now go back and fix the typo on the value of `userInputAge`. Is there anything about the value printed that seems off? Print `userAge` again, but this time unwrap `userAge` using the force unwrap operator. */ - +print(userAge!) /*: Now use optional binding to unwrap `userAge`. If `userAge` has a value, print it to the console. */ - +if let userAge = userAge { + print(userAge) +} //: page 1 of 6 | [Next: App Exercise - Finding a Heart Rate](@next) diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.swift index 746b8fc..26d369f 100644 --- a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.swift @@ -7,12 +7,14 @@ Declare a variable `heartRate` of type `Int?` and set it to `nil`. Print the value. */ - +var heartRate: Int? = nil +print(heartRate) /*: In this example, if the user fixes the positioning of the heart rate monitor, the app may get a proper heart rate reading. Below, update the value of `heartRate` to 74. Print the value. */ - +heartRate = 74 +print(heartRate) /*: As you've done in other app exercises, create a variable `hrAverage` of type `Int` and use the values stored below and the value of `heartRate` to calculate an average heart rate. @@ -21,7 +23,13 @@ let oldHR1 = 80 let oldHR2 = 76 let oldHR3 = 79 let oldHR4 = 70 - +if let heartRate = heartRate { + let hrAverage = (oldHR1 + oldHR2 + oldHR3 + oldHR4 + heartRate) / 5 + print(hrAverage) +} else { + let hrAverage = (oldHR1 + oldHR2 + oldHR3 + oldHR4) / 4 + print(hrAverage) +} /*: If you didn't unwrap the value of `heartRate`, you've probably noticed that you cannot perform mathematical operations on an optional value. You will first need to unwrap `heartRate`. diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/3. Exercise - Functions and Optionals.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/3. Exercise - Functions and Optionals.xcplaygroundpage/Contents.swift index 798d991..8dc753f 100644 --- a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/3. Exercise - Functions and Optionals.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/3. Exercise - Functions and Optionals.xcplaygroundpage/Contents.swift @@ -4,11 +4,27 @@ If an app asks for a user's age, it may be because the app requires a user to be over a certain age to use some of the services it provides. Write a function called `checkAge` that takes one parameter of type `String`. The function should try to convert this parameter into an `Int` value and then check if the user is over 18 years old. If he/she is old enough, print "Welcome!", otherwise print "Sorry, but you aren't old enough to use our app." If the `String` parameter cannot be converted into an `Int` value, print "Sorry, something went wrong. Can you please re-enter your age?" Call the function and pass in `userInputAge` below as the single parameter. Then call the function and pass in a string that can be converted to an integer. */ let userInputAge: String = "34e" +func checkAge(age: String) -> Int? { + if let age = Int(age) { + if age >= 18 { + print("Welcome!") + } else { + print("Sorry, but you aren't old enough to use our app.") + } + return age + } else { + print("Sorry, something went wrong. Can you please re-enter your age?") + return nil + } +} +checkAge(age: userInputAge) +print(checkAge(age: "15")) /*: Go back and update your function to return the age as an integer. Will your function always return a value? Make sure your return type accurately reflects this. Call the function and print the return value. */ - +print(checkAge(age: userInputAge)) +print(checkAge(age: "20")) /*: Imagine you are creating an app for making purchases. Write a function that will take the name of an item for purchase and will return the cost of that item. In the body of the function, check to see if the item is in stock by accessing it in the dictionary `stock`. If it is, return the price of the item by accessing it in the dictionary `prices`. If the item is out of stock, return `nil`. Call the function and pass in a `String` that exists in the dictionaries below. Print the return value. @@ -17,4 +33,18 @@ var prices = ["Chips": 2.99, "Donuts": 1.89, "Juice": 3.99, "Apple": 0.50, "Bana var stock = ["Chips": 4, "Donuts": 0, "Juice": 12, "Apple": 6, "Banana": 6, "Broccoli": 3] +func price(item: String) -> Double? { + if let stock = stock[item] { + if stock > 0 { + return prices[item] + } else { + return nil + } + } else { + return nil + } +} + +print(price(item: "Donuts")) +print(price(item: "Juice")) //: [Previous](@previous) | page 3 of 6 | [Next: App Exercise - Food Functions](@next) diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/4. App Exercise - Food Functions .xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/4. App Exercise - Food Functions .xcplaygroundpage/Contents.swift index 98391a4..8b40b57 100644 --- a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/4. App Exercise - Food Functions .xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/4. App Exercise - Food Functions .xcplaygroundpage/Contents.swift @@ -15,12 +15,26 @@ struct Meal { var meals: [String: Meal] = ["Breakfast": Meal(food: ["Bagel", "Orange Juice", "Egg Whites"], calories: 530)] +func whatMeal(meal: String) -> Meal? { + return meals[meal] +} + +print(whatMeal(meal: "Lunch")) +print(whatMeal(meal: "Breakfast")) /*: iOS comes with a few different APIs for persistence, or saving data. You'll learn more about persistence in another lesson, but for now imagine what an app experience would be like if every time you opened the app all of your data was gone. That would be frustrating, right? Write a function that will check to see if your meal log (a dictionary like that in the previous exercise) is saved to the device. If it is, return the meal log. If it isn't, return an empty dictionary of type `[String: Any]`. The code you should use in this exercise for retrieving something saved to the device is `UserDefaults.standard.dictionary(forKey: "mealLog")`. This code will return an optional `[String: Any]`. If it returns a value, that is your meal log. If it returns `nil`, then no meal log has been saved. Call the function and print the return value. */ +func getmealLog() -> [String: Any] { + if let log = UserDefaults.standard.dictionary(forKey: "mealLog") { + return log + } else { + return [:] + } +} +print(getmealLog()) //: [Previous](@previous) | page 4 of 6 | [Next: Exercise - Failable Initializers](@next) diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/5. Exercise - Failable Initializers.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/5. Exercise - Failable Initializers.xcplaygroundpage/Contents.swift index 9a056cd..1db77f3 100644 --- a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/5. Exercise - Failable Initializers.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/5. Exercise - Failable Initializers.xcplaygroundpage/Contents.swift @@ -3,11 +3,33 @@ Create a `Computer` struct with two properties, `ram` and `yearManufactured`, where both parameters are of type `Int`. Create a failable initializer that will only create an instance of `Computer` if `ram` is greater than 0, and if `yearManufactured` is greater than 1970, and less than 2017. */ +struct Computer { + var ram: Int + var yearManufactured: Int + init?(ram: Int, yearManufactured: Int) { + if ram > 0 && yearManufactured > 1970 && yearManufactured < 2017 { + self.ram = ram + self.yearManufactured = yearManufactured + } else { + return nil + } + } +} /*: Create two instances of `Computer?` using the failable initializer. One instance should use values that will have a value within the optional, and the other should result in `nil`. Use if-let syntax to unwrap each of the `Computer?` objects and print the `ram` and `yearManufactured` if the optional contains a value. */ +let failComputer = Computer(ram: 0, yearManufactured: 1960) +if let computer = failComputer { + print(computer.ram) + print(computer.yearManufactured) +} +let succComputer = Computer(ram: 16, yearManufactured: 2010) +if let computer = succComputer { + print(computer.ram) + print(computer.yearManufactured) +} //: [Previous](@previous) | page 5 of 6 | [Next: App Exercise - Workout or Nil](@next) diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/6. App Exercise - Workout or Nil.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/6. App Exercise - Workout or Nil.xcplaygroundpage/Contents.swift index ab9f71e..7079776 100644 --- a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/6. App Exercise - Workout or Nil.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/6. App Exercise - Workout or Nil.xcplaygroundpage/Contents.swift @@ -9,11 +9,35 @@ Write a failable initializer that takes parameters for your start and end times, and then checks to see if they are fewer than 10 seconds apart. If they are, your initializer should fail. Otherwise, they should set the properties accordingly. */ - +struct Workout { + var startTime: Double + var endTime: Double + + init?(startTime: Double, endTime: Double) { + if endTime - startTime < 10 { + return nil + } else { + self.startTime = startTime + self.endTime = endTime + } + } +} /*: Try to initialize two instances of a `Workout` object and print each of them. One of them should not be initialized because the start and end times are too close together. The other should successfully initialize a `Workout` object. */ +let badWorkout = Workout(startTime: 50000, endTime: 50002) +if let workout = badWorkout { + print(workout.startTime) + print(workout.endTime) +} + +let goodWorkout = Workout(startTime: 50000, endTime: 50500) +if let workout = goodWorkout { + print(workout.startTime) + print(workout.endTime) +} + /*: diff --git a/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.swift index 31ebae6..0102e0a 100644 --- a/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.swift @@ -3,26 +3,71 @@ Create a collection of type [Any], including a few doubles, integers, strings, and booleans within the collection. Print the contents of the collection. */ - +let anyCollection : [Any] = ["I don't know what to write", 3.3, true, false, 4, 2.0 ,"Happy programming"] +print(anyCollection) /*: Loop through the collection. For each integer, print "The integer has a value of ", followed by the integer value. Repeat the steps for doubles, strings and booleans. */ +for anything in anyCollection{ + if let anything = anything as? Int { + print("The integer has a value of \(anything)") + } else if let anything = anything as? Double { + print("The Double has a value of \(anything)") + } else if let anything = anything as? Bool { + print("The Boolean has a value of \(anything)") + } else if let anything = anything as? String { + print("The String has a value of \(anything)") + } +} /*: Create a [String : Any] dictionary, where the values are a mixture of doubles, integers, strings, and booleans. Print the key/value pairs within the collection */ - +let anyCollection2: [String: Any] = ["first": false, "second": 1.0, "third": "90", "fourth":2] +print(anyCollection2) /*: Create a variable `total` of type `Double` set to 0. Then loop through the dictionary, and add the value of each integer and double to your variable's value. For each string value, add 1 to the total. For each boolean, add 2 to the total if the boolean is `true`, or subtract 3 if it's `false`. Print the value of `total`. */ +var total: Double = 0 +for (_, anything) in anyCollection2 { + if let anything = anything as? Bool { + if anything { + total += 2 + } else { + total -= 3 + } + } else if let anything = anything as? Double { + total += anything + } else if let anything = anything as? Int { + total += Double(anything) + } else if let _ = anything as? String { + total += 1 + } +} +print(total) /*: Create a variable `total2` of type `Double` set to 0. Loop through the collection again, adding up all the integers and doubles. For each string that you come across during the loop, attempt to convert the string into a number, and add that value to the total. Ignore booleans. Print the total. */ +var total2: Double = 0 +for (_, anything) in anyCollection2 { + if anything is Bool { + continue + + } else if let anything = anything as? Double { + total2 += anything + } else if let anything = anything as? Int { + total2 += Double(anything) + } else if let anything = anything as? String, + let anythingNum = Double(anything) { + total2 += anythingNum + } +} +print(total2) //: page 1 of 2 | [Next: App Exercise - Workout Types](@next) diff --git a/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/2. App Exercise - Workout Types.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/2. App Exercise - Workout Types.xcplaygroundpage/Contents.swift index ae7131a..5d6e75c 100644 --- a/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/2. App Exercise - Workout Types.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/2. App Exercise - Workout Types.xcplaygroundpage/Contents.swift @@ -45,12 +45,24 @@ var workouts: [Workout] = [ /*: Write simple functions called `describeRun(runningWorkout:)` and `describeSwim(swimmingWorkout:)` that take a `Run` object and a `Swim` object, respectively. Neither should return values. Each function should print a description of the workout, including the run's cadence or the swim's stroke. Time is represented in seconds, distance is represented in meters, and cadence is represented in steps per minute. */ +func describeRun(runningWorkout: Run) { + print("distance: \(runningWorkout.distance), Time: \(runningWorkout.time), cadence: \(runningWorkout.cadence) ") +} +func describeSwim(swimmingWorkout: Swim) { + print("swimming stroke: \(swimmingWorkout.stroke), distance: \(swimmingWorkout.distance), Time: \(swimmingWorkout.time) ") +} /*: Now loop through each workout in `workouts` and, using type casting, call either `describeRun(runningWorkout:)` or `describeSwim(swimmingWorkout:)` on each. Observe what is printed to the console. */ - +for workout in workouts { + if let run = workout as? Run { + describeRun(runningWorkout: run) + } else if let swim = workout as? Swim { + describeSwim(swimmingWorkout: swim) + } +} /*: diff --git a/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/1. Exercise - Guard Statements.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/1. Exercise - Guard Statements.xcplaygroundpage/Contents.swift index f851c0a..4a91d2a 100644 --- a/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/1. Exercise - Guard Statements.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/1. Exercise - Guard Statements.xcplaygroundpage/Contents.swift @@ -4,12 +4,29 @@ import UIKit Imagine you want to write a function to calculate the area of a rectangle. However, if you pass a negative number into the function, you don't want it to calculate a negative area. Create a function called `calculateArea` that takes two `Double` parameters, `x` and `y`, and returns an optional `Double`. Write a guard statement at the beginning of the function that verifies each of the parameters is greater than zero and returns `nil` if not. When the guard has succeeded, calculate the area by multiplying `x` and `y` together, then return the area. Call the function once with positive numbers and once with at least one negative number. */ +func calculateArea(x: Double, y: Double) -> Double? { + guard x > 0 && y > 0 else { return nil } + + return x*y +} +calculateArea(x: 3, y: 2) +calculateArea(x: 2, y: -1) /*: Create a function called `add` that takes two optional integers as parameters and returns an optional integer. You should use one `guard` statement to unwrap both optional parameters, returning `nil` in the `guard` body if one or both of the parameters doesn't have a value. If both parameters can successfully be unwrapped, return their sum. Call the function once with non-`nil` numbers and once with at least one parameter being `nil`. */ +func add(x: Int?, y: Int?) -> Int? { + guard let x = x, + let y = y else { + return nil + } + + return x + y +} +add(x: 1, y: nil) +add(x: 3, y: 6) /*: When working with UIKit objects, you will occasionally need to unwrap optionals to handle user input. For example, the text fields initialized below have `text` properties that are of type `String?`. @@ -29,11 +46,21 @@ let ageTextField = UITextField() firstNameTextField.text = "Jonathan" lastNameTextField.text = "Sanders" ageTextField.text = "28" - +func createUser() -> User? { + guard let firstName = firstNameTextField.text, + let lastName = lastNameTextField.text, + let age = ageTextField.text else { + return nil + } + return User(firstName: firstName, lastName: lastName, age: age) +} /*: Call the function you made above and capture the return value. Unwrap the `User` with standard optional binding and print a statement using each of its properties. */ - +let user = createUser() +if let user = user { + print("\(user.firstName) \(user.lastName) is \(user.age).") +} //: page 1 of 2 | [Next: App Exercise - Guard](@next) diff --git a/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/2. App Exercise - Guard.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/2. App Exercise - Guard.xcplaygroundpage/Contents.swift index 4fea0bf..bdd9ab8 100644 --- a/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/2. App Exercise - Guard.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/3 - Guard/lab/Lab - Guard.playground/Pages/2. App Exercise - Guard.xcplaygroundpage/Contents.swift @@ -10,7 +10,17 @@ import UIKit Write a failable initializer that takes parameters for your start and end times, and then checks to see if they are greater than 10 seconds apart using a guard statement. If they are, your initializer should fail. Otherwise, the initializer should set the properties accordingly. */ +struct Workout { + var startTime: Double + var endTime: Double + init?(startTime: Double, endTime: Double) { + guard endTime - startTime > 10 else { return nil } + + self.startTime = startTime + self.endTime = endTime + } +} /*: Imagine a screen where a user inputs a meal that they've eaten. If the user taps a "save" button without adding any food, you might want to prompt the user that they haven't actually added anything. @@ -28,11 +38,22 @@ let caloriesTextField = UITextField() foodTextField.text = "Banana" caloriesTextField.text = "23" +func logFood() -> Food? { + guard let foodName = foodTextField.text, + let caloriesText = caloriesTextField.text, + let calories = Int(caloriesText) else { + return nil + } + + return Food(name: foodName, calories: calories) +} /*: Call the function you made above and capture the return value. Unwrap the `Food` object with standard optional binding and print a statement about the food using each of its properties. Go back and change the text in `caloriesTextField` to a string that cannot be converted into a number. What happens in that case? */ - +if let food = logFood() { + print("\(food.name) has \(food.calories) calories.") +} /*: diff --git a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.swift index fb002fb..efaa915 100644 --- a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.swift @@ -8,6 +8,7 @@ for _ in 0..<10 { print("The value of foo is \(foo)") } //print("The value of foo is \(foo)") +// foo is inside the for cannot seen from outside /*: @@ -19,17 +20,35 @@ for _ in 0..<10 { print("The value of x is \(x)") } print("The final value of x is \(x)") +//because x is declared outside the for /*: In the body of the function `greeting` below, use variable shadowing when unwrapping `greeting`. If `greeting` is successfully unwrapped, print a statement that uses the given greeting to greet the given name (i.e. if `greeting` successfully unwraps to have the value "Hi there" and `name` is `Sara`, print "Hi there, Sara."). Otherwise, use "Hello" to print a statement greeting the given name. Call the function twice, once passing in a value for greeting, and once passing in `nil`. */ func greeting(greeting: String?, name: String) { - + if let greeting = greeting { + print("\(greeting), \(name).") + } else { + print("Hello, \(name).") + } } +greeting(greeting: "Hi there", name: "Jessica") +greeting(greeting: nil, name: "Bob") /*: Create a class called `Car`. It should have properties for `make`, `model`, and `year` that are of type `String`, `String`, and `Int`, respectively. Since this is a class, you'll need to write your own memberwise initializer. Use shadowing when naming parameters in your initializer. */ +class Car { + let make: String + let model: String + let year: Int + + init(make: String, model: String, year: Int) { + self.make = make + self.model = model + self.year = year + } +} //: page 1 of 2 | [Next: App Exercise - Step Competition](@next) diff --git a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.swift index a4c5c73..a9d73a7 100644 --- a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.swift @@ -8,6 +8,18 @@ struct User { var name: String var stepsToday: Int + init(name: String, stepsToday: Int) { + self.name = name + self.stepsToday = stepsToday + } + + init?(name: String?, stepsToday: Int?) { + guard let name = name, + let stepsToday = stepsToday else {return nil} + + self.name = name + self.stepsToday = stepsToday + } } let stepMaster = User(name: "StepMaster", stepsToday: 8394) @@ -24,8 +36,8 @@ func getWinner(competitors: [User]) -> User? { var topCompetitor: User? for competitor in competitors { - if let topCompetitor = topCompetitor { - if competitor.stepsToday > topCompetitor.stepsToday { + if let currentWinner = topCompetitor { + if competitor.stepsToday > currentWinner.stepsToday { topCompetitor = competitor } } else { @@ -34,6 +46,9 @@ func getWinner(competitors: [User]) -> User? { } return topCompetitor } + +let winner = getWinner(competitors: competitors) +print(winner?.name ?? "") /*: Write a memberwise initializer inside the `User` struct above that uses variable shadowing for naming the parameters of the initializer. */ diff --git a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.swift index dc7d938..fd58221 100644 --- a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.swift @@ -3,31 +3,75 @@ Define a `Suit` enum with four possible cases: `clubs`, `spades`, `diamonds`, and `hearts`. */ - +enum Suit { + case clubs + case spades + case diamonds + case hearts +} /*: Imagine you are being shown a card trick and have to draw a card and remember the suit. Create a variable instance of `Suit` called `cardInHand` and assign it to the `hearts` case. Print out the instance. */ - +var cardInHand = Suit.hearts +print(cardInHand) /*: Now imagine you have to put back the card you drew and draw a different card. Update the variable to be a spade instead of a heart. */ - +cardInHand = .spades /*: Imagine you are writing an app that will display a fun fortune (i.e. something like "You will soon find what you seek.") based on cards drawn. Write a function called `getFortune(cardSuit:)` that takes a parameter of type `Suit`. Inside the body of the function, write a switch statement based on the value of `cardSuit`. Print a different fortune for each `Suit` value. Call the function a few times, passing in different values for `cardSuit` each time. */ +func getFortune(cardSuit: Suit) { + switch cardSuit { + case .clubs: + print("You will be hungry very soon.") + case .spades: + print("Happiness is in your future.") + case .diamonds: + print("Take risks to get your dreams.") + case .hearts: + print("You may make your decision with confidence in the next few days.") + } +} +getFortune(cardSuit: .clubs) +getFortune(cardSuit: .hearts) +getFortune(cardSuit: .diamonds) /*: Create a `Card` struct below. It should have two properties, one for `suit` of type `Suit` and another for `value` of type `Int`. */ - +struct Card { + enum Value { + case ace + case one + case two + case three + case four + case five + case six + case seven + case eight + case nine + case ten + case jack + case queen + case king + } + + var suit: Suit + var value: Value +} /*: How many values can playing cards have? How many values can `Int` be? It would be safer to have an enum for the card's value as well. Inside the struct above, create an enum for `Value`. It should have cases for `ace`, `two`, `three`, `four`, `five`, `six`, `seven`, `eight`, `nine`, `ten`, `jack`, `queen`, `king`. Change the type of `value` from `Int` to `Value`. Initialize two `Card` objects and print a statement for each that details the card's value and suit. */ - +let fourOfHearts = Card(suit: .hearts, value: .four) +let sixOfClubs = Card(suit: .clubs, value: .six) +print("The \(fourOfHearts.value) of \(fourOfHearts.suit).") +print("The \(sixOfClubs.value) of \(sixOfClubs.suit).") //: page 1 of 2 | [Next: App Exercise - Swimming Workouts](@next) diff --git a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.swift index de34f32..b15eb11 100644 --- a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.swift @@ -5,12 +5,42 @@ Previous app exercises have introduced the idea that your fitness tracking app may allow users to track swimming workouts. Create a `SwimmingWorkout` struct below with properties for `distance`, `time`, and `stroke`. `distance` and `time` should be of type `Double` and will represent distance in meters and time in seconds, and `stroke` should be of type `String`. */ +struct SwimmingWorkout { + enum Stroke { + case freestyle + case butterfly + case backstroke + case breaststroke + } + static var freestyleWorkouts: [SwimmingWorkout] = [] + static var butterflyWorkouts: [SwimmingWorkout] = [] + static var backstrokeWorkouts: [SwimmingWorkout] = [] + static var breaststrokeWorkouts: [SwimmingWorkout] = [] + + var distance: Double + var time: Double + var stroke: Stroke + + func save() { + switch stroke { + case .freestyle: + SwimmingWorkout.freestyleWorkouts.append(self) + case .butterfly: + SwimmingWorkout.butterflyWorkouts.append(self) + case .backstroke: + SwimmingWorkout.backstrokeWorkouts.append(self) + case .breaststroke: + SwimmingWorkout.breaststrokeWorkouts.append(self) + } + } +} /*: Allowing `stroke` to be of type `String` isn't very type-safe. Inside the `SwimmingWorkout` struct, create an enum called `Stroke` that has cases for `freestyle`, `butterfly`, `backstroke`, and `breaststroke`. Change the type of `stroke` from `String` to `Stroke`. Create two instances of `SwimmingWorkout` objects. */ - +let freestyleWorkout = SwimmingWorkout(distance: 500, time: 3000, stroke: .freestyle) +let butterflyWorkout = SwimmingWorkout(distance: 500, time: 3400, stroke: .butterfly) /*: Now imagine you want to log swimming workouts separately based on the swimming stroke. You might use arrays as static variables on `SwimmingWorkout` for this. Add four static variables, `freestyleWorkouts`, `butterflyWorkouts`, `backstrokeWorkouts`, and `breaststrokeWorkouts`, to `SwimmingWorkout` above. Each should be of type `[SwimmingWorkout]` and should default to empty arrays. @@ -20,7 +50,10 @@ /*: Now add an instance method to `SwimmingWorkout` called `save()` that takes no parameters and has no return value. This method will add its instance to the static array on `SwimmingWorkout` that corresponds to its swimming stroke. Inside `save()` write a switch statement that switches on the instance's `stroke` property, and appends `self` to the proper array. Call save on the two instances of `SwimmingWorkout` that you created above, and then print the array(s) to which they should have been added to see if your `save` method works properly. */ - +freestyleWorkout.save() +butterflyWorkout.save() +print(SwimmingWorkout.freestyleWorkouts) +print(SwimmingWorkout.butterflyWorkouts) /*: diff --git a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.swift b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.swift index f54a018..66d69c0 100644 --- a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.swift +++ b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.swift @@ -4,26 +4,55 @@ Create a `Human` class with two properties: `name` of type `String`, and `age` of type `Int`. You'll need to create a memberwise initializer for the class. Initialize two `Human` instances. */ - +class Human : CustomStringConvertible, Equatable, Comparable, Codable{ + static func < (lhs: Human, rhs: Human) -> Bool { + return lhs.age < rhs.age + } + + static func == (lhs: Human, rhs: Human) -> Bool { + return lhs.name == rhs.name && lhs.age == rhs.age + } + + var description: String { + return "name: \(name), age: \(age)" + } + +let name: String +var age: Int + +init(name: String, age: Int) { + self.name = name + self.age = age +} +} +let atheer = Human(name: "Atheer", age: 21) +let noura = Human(name: "Noura", age: 20) /*: Make the `Human` class adopt the `CustomStringConvertible`. Print both of your previously initialized `Human` objects. */ - +print(atheer) +print(noura) /*: Make the `Human` class adopt the `Equatable` protocol. Two instances of `Human` should be considered equal if their names and ages are identical to one another. Print the result of a boolean expression evaluating whether or not your two previously initialized `Human` objects are equal to eachother (using `==`). Then print the result of a boolean expression evaluating whether or not your two previously initialized `Human` objects are not equal to eachother (using `!=`). */ +print(noura==atheer) +print(noura != atheer) /*: Make the `Human` class adopt the `Comparable` protocol. Sorting should be based on age. Create another three instances of a `Human`, then create an array called `people` of type `[Human]` with all of the `Human` objects that you have initialized. Create a new array called `sortedPeople` of type `[Human]` that is the `people` array sorted by age. */ +let people = [Human(name: "banna", age: 20), Human(name: "sarah", age: 70), Human(name: "Saleh", age: 40)] - +let sortedPeople = people.sorted(by: <) /*: Make the `Human` class adopt the `Codable` protocol. Create a `JSONEncoder` and use it to encode as data one of the `Human` objects you have initialized. Then use that `Data` object to initialize a `String` representing the data that is stored, and print it to the console. */ import Foundation - +let jsonEncoder = JSONEncoder() +let data = try! jsonEncoder.encode(noura) +let string = String(data: data, encoding: .utf8)! +print(string) //: page 1 of 5 | [Next: App Exercise - Printable Workouts](@next) diff --git a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.swift b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.swift index db03100..668dfcc 100644 --- a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.swift +++ b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.swift @@ -5,7 +5,7 @@ The `Workout` objects you have created so far in app exercises don't show a whole lot of useful information when printed to the console. They also aren't very easy to compare or sort. Throughout these exercises, you'll make the `Workout` class below adopt certain protocols that will solve these issues. */ -class Workout { +class Workout: CustomStringConvertible, Equatable, Comparable, Codable { var distance: Double var time: Double var identifier: Int @@ -15,29 +15,51 @@ class Workout { self.time = time self.identifier = identifier } + + var description: String { + return "Workout \(identifier): \(distance) meters in \(time) seconds." + } + + static func ==(lhs: Workout, rhs: Workout) -> Bool { + return lhs.identifier == rhs.identifier + } + + static func <(lhs: Workout, rhs: Workout) -> Bool { + return lhs.identifier < rhs.identifier + } } /*: Make the `Workout` class above conform to the `CustomStringConvertible` protocol so that printing an instance of `Workout` will provide useful information in the console. Create an instance of `Workout`, give it an identifier of 1, and print it to the console. */ - +let wokout1 = Workout(distance: 999, time: 110, identifier: 1) +print(wokout1) /*: Make the `Workout` class above conform to the `Equatable` protocol. Two `Workout` objects should be considered equal if they have the same identifier. Create another instance of `Workout`, giving it an identifier of 2, and print a boolean expression that evaluates to whether or not it is equal to the first `Workout` instance you created. */ - +let wokout2 = Workout(distance: 900, time: 90, identifier: 2) +print(wokout2 == wokout1) /*: Make the `Workout` class above conform to the `Comparable` protocol so that you can easily sort multiple instances of `Workout`. `Workout` objects should be sorted based on their identifier. Create three more `Workout` objects, giving them identifiers of 3, 4, and 5, respectively. Then create an array called `workouts` of type `[Workout]` and assign it an array literal with all five `Workout` objects you have created. Place these objects in the array out of order. Then create another array called `sortedWorkouts` of type `[Workout]` that is the `workouts` array sorted by identifier. */ +let wokout3 = Workout(distance: 700, time: 77, identifier: 3) +let wokout4 = Workout(distance: 1300, time: 130, identifier: 4) +let wokout5 = Workout(distance: 2222, time: 150, identifier: 5) +let workouts = [wokout1, wokout2, wokout3, wokout4, wokout5] +let sortedWorkouts = workouts.sorted(by: <) /*: Make `Workout` adopt the `Codable` protocol so you can easily encode `Workout` objects as data that can be stored between app launches. Use a `JSONEncoder` to encode one of your `Workout` instances. Then use the encoded data to initialize a `String`, and print it to the console. */ import Foundation - +let jsonEncoder = JSONEncoder() +let data = try! jsonEncoder.encode(wokout1) +let string = String(data: data, encoding: .utf8)! +print(string) //: [Previous](@previous) | page 2 of 5 | [Next: Exercise - Create a Protocol](@next) diff --git a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/3. Exercise - Create a Protocol.xcplaygroundpage/Contents.swift b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/3. Exercise - Create a Protocol.xcplaygroundpage/Contents.swift index 23768fb..8912351 100644 --- a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/3. Exercise - Create a Protocol.xcplaygroundpage/Contents.swift +++ b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/3. Exercise - Create a Protocol.xcplaygroundpage/Contents.swift @@ -3,16 +3,40 @@ Create a protocol called `Vehicle` with two requirements: a nonsettable `numberOfWheels` property of type `Int`, and a function called `drive()`. */ - - +protocol Vehicle { + var numberOfWheels: Int {get} + func drive() +} /*: Define a `Car` struct that implements the `Vehicle` protocol. `numberOfWheels` should return a value of 4, and `drive()` should print "Vroom, vroom!" Create an instance of `Car`, print its number of wheels, then call `drive()`. */ +struct Car: Vehicle { + var numberOfWheels: Int { + return 4 + } + + func drive() { + print("Vroom, vroom!") + } +} +let car = Car() +car.drive() /*: Define a `Bike` struct that implements the `Vehicle` protocol. `numberOfWheels` should return a value of 2, and `drive()` should print "Begin pedaling!". Create an instance of `Bike`, print its number of wheels, then call `drive()`. */ +struct Bike: Vehicle { + var numberOfWheels: Int { + return 2 + } + + func drive() { + print("Begin pedaling!") + } +} +let bike = Bike() +bike.drive() //: [Previous](@previous) | page 3 of 5 | [Next: App Exercise - Similar Workouts](@next) diff --git a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/4. App Exercise - Similar Workouts.xcplaygroundpage/Contents.swift b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/4. App Exercise - Similar Workouts.xcplaygroundpage/Contents.swift index 1fc51be..3e8e9bd 100644 --- a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/4. App Exercise - Similar Workouts.xcplaygroundpage/Contents.swift +++ b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/4. App Exercise - Similar Workouts.xcplaygroundpage/Contents.swift @@ -7,14 +7,33 @@ Create a protocol `Workout` with two requirements: a settable property called `distance` and a settable property called `time`, both of type `Double`. */ - +protocol Workout { + var distance: Double {get set} + var time: Double {get set} +} /*: Create two structs, `RunningWorkout` and `SwimmingWorkout`. Both should conform to the `Workout` protocol. Uncomment the function `simpleWorkoutDescription` below, create an instance of each of your structs, and call `simpleWorkoutDescription` twice, passing in a `RunningWorkout` object and then a `SwimmingWorkout` object. */ -//func simpleWorkoutDescription(workout: Workout) { -// print("You went \(workout.distance) meters in \(workout.time) seconds.") -//} +func simpleWorkoutDescription(workout: Workout) { + print("You went \(workout.distance) meters in \(workout.time) seconds.") +} + +struct RunningWorkout: Workout { + var distance: Double + var time: Double +} + +struct SwimmingWorkout: Workout { + var distance: Double + var time: Double +} + +let run = RunningWorkout(distance: 3000, time: 200) +let swim = SwimmingWorkout(distance: 400, time: 40) + +simpleWorkoutDescription(workout: run) +simpleWorkoutDescription(workout: swim) //: [Previous](@previous) | page 4 of 5 | [Next: App Exercise - Heart Rate Delegate](@next) diff --git a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/5. App Exercise - Heart Rate Delegate.xcplaygroundpage/Contents.swift b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/5. App Exercise - Heart Rate Delegate.xcplaygroundpage/Contents.swift index b62e459..a525407 100644 --- a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/5. App Exercise - Heart Rate Delegate.xcplaygroundpage/Contents.swift +++ b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/5. App Exercise - Heart Rate Delegate.xcplaygroundpage/Contents.swift @@ -8,17 +8,24 @@ import UIKit `HeartRateViewController` below is a view controller that will present the heart rate information to the user. Throughout the exercises below you'll use the delegate pattern to pass information from an instance of `HeartRateReceiver` to the view controller so that anytime new information is obtained it is presented to the user. */ +protocol HeartRateReceiverDelegate { + func heartRateUpdated(to bpm: Int) +} + class HeartRateReceiver { var currentHR: Int? { didSet { if let currentHR = currentHR { print("The most recent heart rate reading is \(currentHR).") + delegate?.heartRateUpdated(to: currentHR) } else { print("Looks like we can't pick up a heart rate.") } } } + var delegate: HeartRateReceiverDelegate? + func startHeartRateMonitoringExample() { for _ in 1...10 { let randomHR = 60 + Int(arc4random_uniform(UInt32(15))) @@ -28,13 +35,21 @@ class HeartRateReceiver { } } -class HeartRateViewController: UIViewController { +class HeartRateViewController: UIViewController, HeartRateReceiverDelegate { var heartRateLabel: UILabel = UILabel() + + func heartRateUpdated(to bpm: Int) { + heartRateLabel.text = "\(bpm)" + print("The user has been shown a heart rate of \(bpm).") + } } /*: First, create an instance of `HeartRateReceiver` and call `startHeartRateMonitoringExample`. Notice that every two seconds `currentHR` get set and prints the new heart rate reading to the console. */ - +let heartReceiver = HeartRateReceiver() +let viewController = HeartRateViewController() +heartReceiver.delegate = viewController +heartReceiver.startHeartRateMonitoringExample() /*: In a real app, printing to the console does not show information to the user. You need a way of passing information from the `HeartRateReceiver` to the `HeartRateViewController`. To do this, create a protocol called `HeartRateReceiverDelegate` that requires a method `heartRateUpdated(to bpm:)` where `bpm` is of type `Int` and represents the new rate as _beats per minute_. Since playgrounds read from top to bottom and the two previously declared classes will need to use this protocol, you'll need to declare this protocol above the declaration of `HeartRateReceiver`. @@ -56,4 +71,4 @@ class HeartRateViewController: UIViewController { _THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE._ */ -//:[Previous](@previous) | page 5 of 5 \ No newline at end of file +//:[Previous](@previous) | page 5 of 5 diff --git a/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/1. Exercise - Create Closures.xcplaygroundpage/Contents.swift b/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/1. Exercise - Create Closures.xcplaygroundpage/Contents.swift index 54b0bf4..b7e174c 100644 --- a/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/1. Exercise - Create Closures.xcplaygroundpage/Contents.swift +++ b/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/1. Exercise - Create Closures.xcplaygroundpage/Contents.swift @@ -3,16 +3,31 @@ Create a closure assigned to a constant `blankClosure` that has no parameters and no return value. */ +let blankClosure = { () in +} /*: Create a closure assigned to a constant `fourClosure` that has no parameters and returns an `Int`. The body of the closure should always return the value 4. Call the closure four times. */ +let fourClosure = { () -> Int in + return 4 +} - +fourClosure() +fourClosure() +fourClosure() +fourClosure() /*: Create a closure assigned to a constant `greeting` that accepts a `name` string argument with no return value. Within the body of the closure, print the argument. Call the closure four times using "Gary", "Jane", "Rick", and "Beth" as arguments. */ +let greeting = { (name: String) in + print("Hi, \(name).") +} +greeting("Gary") +greeting("Jane") +greeting("Rick") +greeting("Beth") //: page 1 of 3 | [Next: Exercise - Passing Closures as Arguments and Syntactic Sugar](@next) diff --git a/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/2. Exercise - Collection Functions.xcplaygroundpage/Contents.swift b/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/2. Exercise - Collection Functions.xcplaygroundpage/Contents.swift index 8df00f1..7bce709 100644 --- a/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/2. Exercise - Collection Functions.xcplaygroundpage/Contents.swift +++ b/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/2. Exercise - Collection Functions.xcplaygroundpage/Contents.swift @@ -3,21 +3,33 @@ Define a function `forwards` that has two `String` arguments, `name1` and `name2`, and returns a `Bool`. Within the definition of the function, print each of the arguments, then return whether or not `name1` is less than `name2`. You can use comparison operators, such as `<` and `>`, to compare alphabetical order in strings. */ - +func forwards(name1: String, name2: String) -> Bool { + print("name 1 : \(name1)") + print("name 2 : \(name2)") + return name1 < name2 +} /*: Create a `[String]` collection using names of your friends and family. Call the collection's `sorted(by:)` function, passing in `forwards` as the argument. Store the result into `sortedCollection1`, then print the result. This should sort your collection alphabetically. */ +let family = ["Manar", "Noura", "Atheer",] +let sortedCollection1 = family.sorted(by: forwards) +print(sortedCollection1) /*: Using your initial collection of unsorted friends and family names, call the collection's `sorted(by:)` function, but pass in your own closure instead of the `forwards` function. The closure should sort the collection in the same way as `forwards`. Be sure to include parameter names, parameter types, and the `return` statement within your closure. Store the result in `sortedCollection2`, then print the result. */ +let sortedCollection2 = family.sorted { (name1, name2) -> Bool in + return name1 < name2 +} +print(sortedCollection2) /*: Similar to the previous exercise, call the collection's `sorted(by:)` function, but remove as much of the unnecessary closure syntax as you can. Store the result in `sortedCollection3`, then print the result. */ - +let sortedCollection3 = family.sorted(by: <) +print(sortedCollection3) //: [Previous](@previous) | page 2 of 3 | [Next: Exercise - Collection Functions](@next) diff --git a/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/3. Exercise - Collection Functions.xcplaygroundpage/Contents.swift b/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/3. Exercise - Collection Functions.xcplaygroundpage/Contents.swift index 24f398b..89b806f 100644 --- a/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/3. Exercise - Collection Functions.xcplaygroundpage/Contents.swift +++ b/Student Resources/5 - Working With the Web/1 - Closures/lab/Lab - Closures.playground/Pages/3. Exercise - Collection Functions.xcplaygroundpage/Contents.swift @@ -5,19 +5,24 @@ */ let testScores = [65, 80, 88, 90, 47] +let testScoresAdd = testScores.map { $0 + 1 } +print(testScoresAdd) /*: Using the code below, use the `filter` function to create a new array of `String` values. The new array should only include Strings longer than four characters. Use `$0` as you iterate through the values of the array. Print the resulting collection. */ let schoolSubjects = ["Math", "Computer Science", "Gym", "English", "Biology"] +let longSubjects = schoolSubjects.filter { $0.count > 4 } +print(longSubjects) /*: Using the code below, use the `reduce` function to subtract all of the values within the array from the starting value 100. Print the resulting value. */ let damageTaken = [25, 10, 15, 30, 20] - +let substracringTheDamageTaken = damageTaken.reduce(100) { $0 - $1 } +print(substracringTheDamageTaken) /*: _Copyright © 2018 Apple Inc._ diff --git a/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/1. Exercise - Extensions.xcplaygroundpage/Contents.swift b/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/1. Exercise - Extensions.xcplaygroundpage/Contents.swift index c894363..3ebe376 100644 --- a/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/1. Exercise - Extensions.xcplaygroundpage/Contents.swift +++ b/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/1. Exercise - Extensions.xcplaygroundpage/Contents.swift @@ -3,17 +3,42 @@ Define an extension to `Character` that includes a function `isVowel()`. The function returns `true` if the character is a vowel (a,e,i,o,u), and `false` otherwise. Be sure to properly handle uppercase and lowercase characters. */ - +extension Character { + func isVowel() -> Bool { + switch self { + case "a", "e", "i", "o", "u", "A", "E", "I", "O", "U": + return true + default: + return false + } + } +} /*: Create two `Character` constants, `myVowel` and `myConsonant`, and set them to a vowel and a consonant, respectively. Use the `isVowel()` methods on each constant to determine whether or not it's a vowel. */ - +let myVowel: Character = "u" +let myConsonant: Character = "r" +myVowel.isVowel() +myConsonant.isVowel() /*: Create a `Rectangle` struct with two variable properties, `length` and `width`, both of type `Double`. Below the definition, write an extension to `Rectangle` that includes a function, `half()`. This function returns a new `Rectangle` instance with half the length and half the width of the original rectangle. */ - +struct Rectangle { + var length: Double + var width: Double +} + +extension Rectangle { + func half() -> Rectangle { + return Rectangle(length: length/2, width: width/2) + } + + mutating func halved() { + self = half() + } +} /*: Within the existing `Rectangle` extensions, add a new mutating function, `halved()`, which updates the original rectangle to have half the length and half the width. Use the `half()` function as part of the implementation for `halved()`. @@ -23,6 +48,10 @@ /*: Create a variable `Rectangle` called `myRectangle`, and set its length to 10 and its width to 5. Create a second instance, `mySmallerRectangle`, that's the result of calling `half()` on `myRectangle`. Then update the values of `myRectangle` by calling `halved()` on itself. Print each of the instances. */ - +var myRectangle = Rectangle(length: 10, width: 5) +var mySmallerRectangle = myRectangle.half() +myRectangle.halved() +print(myRectangle) +print(mySmallerRectangle) //: page 1 of 2 | [Next: App Exercise - Workout Extensions](@next) diff --git a/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/2. App Exercise - Workout Extensions.xcplaygroundpage/Contents.swift b/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/2. App Exercise - Workout Extensions.xcplaygroundpage/Contents.swift index 45e304e..cb8dc20 100644 --- a/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/2. App Exercise - Workout Extensions.xcplaygroundpage/Contents.swift +++ b/Student Resources/5 - Working With the Web/2 - Extensions/lab/Lab - Extensions.playground/Pages/2. App Exercise - Workout Extensions.xcplaygroundpage/Contents.swift @@ -10,15 +10,31 @@ struct Workout { var time: Double var averageHR: Int } +extension Workout: CustomStringConvertible { + var description: String { + return "distance: \(distance), time: \(time), averageHR: \(averageHR)" + } +} /*: Now create another extension for `Workout` and add a property `speed` of type `Double`. It should be a computed property that returns the average meters per second traveled during the workout. */ - +extension Workout { + var speed: Double { + return distance / time + } + + func harderWorkout() -> Workout { + return Workout(distance: distance*2, time: time*2, averageHR: averageHR + 40) + } +} /*: Now add a method `harderWorkout` that takes no parameters and returns another `Workout` instance. This method should double the `distance` and `time` properties, and add 40 to `averageHR`. Create an instance of `Workout` and print it to the console. Then call `harderWorkout` and print the new `Workout` instance to the console */ - +let workout = Workout(distance: 1299, time: 125, averageHR: 130) +print(workout) +let harderWorkout = workout.harderWorkout() +print(harderWorkout) /*: