Nick Walter Swift 5 Course - Part 1 (lectures 1 to 15)
Well, this has been the worst effort for a long, long time! An entire month! There's no point fretting about it - that will not achieve anything. I've felt a renewed sense of wanting to achieve with coding. At this point, it makes sense to switch to a new Swift 5 course, as the language has been updated. It will also give me a chance to consolidate and ensure my understanding of the basics is solid. Then I will resume with Angela's course! So not a cop out. OK, let's get stuck in!
Start Time - 14:00
Just looking at Nick's course, there are 6 hours of content here. Perfect for the next few entries - possibly just even the next couple of days' worth. A lot will be the basics, but that's fine.
Intro/Installing Xcode - skipping through all these! Don't need the Linux/Windows ones either.
Swift Basics
This is going from absolute beginner, so rather than totally skipping through and onto Intermediate, I'm going to just do a general skim over.
Some general info about display settings, auto complete. All fine.
No auto complete functionality on Windows! Good job I'm doing this with Mac!
Variables and Constants
All straightforward. Challenge at end of the video - create a variable named candy and set to my favourite candy...
Very, very easy of course!
Strings, Ints and Types
Types are so important - whenever you create a new variable, we cannot change the type that it holds.
Explicit type declaration - mentioning the keyword Int, Double etc.
Challenge - make an int constant that shows the current minute of the time I am at (14:26)
Start Time - 14:00
Just looking at Nick's course, there are 6 hours of content here. Perfect for the next few entries - possibly just even the next couple of days' worth. A lot will be the basics, but that's fine.
Intro/Installing Xcode - skipping through all these! Don't need the Linux/Windows ones either.
Swift Basics
This is going from absolute beginner, so rather than totally skipping through and onto Intermediate, I'm going to just do a general skim over.
Some general info about display settings, auto complete. All fine.
No auto complete functionality on Windows! Good job I'm doing this with Mac!
Variables and Constants
All straightforward. Challenge at end of the video - create a variable named candy and set to my favourite candy...
var candy = "Twirl"
Very, very easy of course!
Strings, Ints and Types
Types are so important - whenever you create a new variable, we cannot change the type that it holds.
Explicit type declaration - mentioning the keyword Int, Double etc.
Challenge - make an int constant that shows the current minute of the time I am at (14:26)
let minute = 26
Obviously this is all very, very easy stuff. It is just a refresher/reminder of things!
Comments and Print
Nick makes the point that one small error in Swift means the whole thing won't work e.g. one missed speech mark. Comments - as a way of explaining what is going alongside the code etc.
Command and forward slash - do this after selecting the code, then it comments out.
Print - key thing is that it's not a physical printing of course but prints information to the screen.
You can print values on the fly e.g print(123) or print("Hi!"). Or you can print and put in the actual variable/constant.
Challenge - print out the variable that holds my lucky number...
var luckyNumber = 3
print(luckyNumber)
Easy of course!
Math, Doubles and Floats
Basically you have different number types - Ints and doubles. If you use doubles in the question, you get them in the answer.
A float is like a double but not as specific.
Never use floats! You may see it in other people's codes but don't need to actually use it. I've always gone for Doubles to be honest. Floats seem archaic.
Converting - you put the type you want before the value and brackets.
var age = 33
var weight = 187.4
Int(weight) / age
Challenge - work out how tall in inches knowing the feet and inches
var feet = 5
var inches = 10
var heightInInches = (feet * 12) + inches
Booleans and If Statements
var canRideWaterSlide = age > 12
Greater than, greater or equal to - all of this is very familiar.
If statements - again, I'm familiar with the syntax here.
if age >= 12 {
print("You can go down the slide!")
} else {
print("Too young!")
}
Technically you can do the below, as I had already created the Boolean. But you tend to do the above.
if canRideWaterSlide {
print("You can go down the slide!")
} else {
print("Too young!")
}
Challenge - pick a random number then if it that number is less than 10, print this is a single digit number.
var randomNumber = 9
if randomNumber < 10 {
print("This is a single digit number")
} else {
print("This number has two digits or more!")
}
Easy peasy. Done the the else clause too!
Else, and, or
So as I've already been doing, here is how to extend the if statement.
Right, so we've covered else. The next step is using and/or.
if heightInInches >= 50 && weight > 100 {
print("You are eligible for the ride!")
} else {
print("Not eligible!")
}
Or - the use of || - only one of the conditions needs to be true. All easy stuff!
Challenge - make an if statement to see if the person's name is Nick and age is 30.
var name = "Buffy"
var age = 22
if name == "Nick" && age == 30 {
print("This is Nick who is thirty years old.")
} else {
print("Not Nick who is 30!")
}
No prob. Nick makes the point that it has to be identical for the string - case sensitive etc.
Arrays
Whizzing through this. Good to go over and nothing taxing. An array is like a list - an ordered list. E.g. top 5 favourite movies.
var top5BuffyEps = ["Becoming", "Restless", "Hush", "Band Candy", "The Gift"]
Accessing these using the index system of 0, 1, 2 etc.
.append - the method of adding a new item. Can also do += with the name of the item...
Also .insert method of where to add it in in a specific place. Or removeAtIndex. Or remove all...
.count - to see the number of items. Again, all good stuff.
Challenge - make a top 3 movie list and put them in an array. Do it as a constant.
let favouriteMovies = ["The Shawshank Redemption", "Groundhog Day", "About A Boy"]
So we're getting towards the end - loops next then a little project! That will do for this entry - all of the basics complete!
Loops
I agree Nick - these are really fun! For are the most common...
for _ in 1...10 {
print("Hi!")
}
for n in 1...12 {
print(n)
}
In the second example, I have assigned a value - that makes it more powerful with options. With the underscore, that's limited.
for episode in top5BuffyEps {
print(episode)
}
var luckyNumbers = [3,5,7,9]
for n in luckyNumbers {
print(n)
}
OK so for the first time I've got caught out! To be fair, the answer was not obvious so here is what Nick did.
for x in 0..<luckyNumbers.count {
print("\(x+1). \(luckyNumbers[x])")
}
Coding Project
Bring it on! So the challenge is to print out how many of the numbers are above 5,000...
var numbers = [6895,1078,4920,410,5058,8167,2797,4742,6091,774,2666,1297,7560,132,9213,3776,3768,6179,8132,2689,4132,3697,579,5014,3347,8341,5880,3804,6154,7309,5292,136,952,690,619,7392,4672,7461,4247,5637,8485,7137,2632,8063,2493,1491,5166,1020,6499,2987,7137,1382,5985,8581,8602,4450,6977,4329,5525,7921,7433,675,7385,7445,4702,6385,6967,249,8782,8856,7025,2074,433,9994,2548,4909,6360,2620,3573,7910,2514,1287,3547,1421,184,5165,1205,1873,5248,7636,2562,6281,7000,7841,2362,8050,9075,3100,5106,1438,]
var total = 0
for n in numbers {
if n > 5000 {
total += 1
}
}
So I got the numbers Array from Nick. I created the total variable which is deliberately at 0, as it will count the number of items that are over 5000. Then added one to the total.
Totally nailed it! Comments were confusing in the Q&A - someone made it very complex and still got it wrong. Anyway, my method was completely without help and validates that it was still worth going over this.
Nick is about to show how to generate random numbers to put in an array but I would like a go first as I don't like the use of the "] etc....
YES! Took a good few minutes but YES!
var numberArray: [Int] = []
for _ in 1...100 {
numberArray += [Int.random(in: 1...10_000)]
}
print(numberArray)
I've also put this as a comment under the Q&A!
Nice to share something as I genuinely believe that's a lot more logical than what Nick did!
Finish Time - 16:11 ( 2 hours 11 minutes)
Really happy with this session. Done all of the 'basics' in one go. Next time (prob tomorrow), will be focusing on the intermediate elements, then an iPhone project to actually get some practice with the labels, buttons etc. Well worth doing and I'm glad that my rebuilding stage, after being very lax of late is going well!
Comments
Post a Comment