Build Your Swift Skills - Lesson 7 - Loops

preview_player
Показать описание

Alright, so now you know how to store collections of data using Arrays and Dictionaries.

What if you need to write code to do something with each piece of data in the collection?

For example, let’s say you need to give each employee in the company database a salary raise? (Yeah, right!)

Well, we had better learn about Loops to make that happen!

Loops come in handy to let you go through each item in the collection and execute some code for each!

Finally, don't forget to download the worksheet below to get extra practice!

To download the code, projects, worksheets and assets, visit:

🚨 SPECIAL CWC+ OFFER FOR YOUTUBE:

FREE BEGINNER COURSE:

CWC+ LEARNING PATH:

CONNECT:

ABOUT CODEWITHCHRIS:

DID THESE LESSONS HELP YOU?
Рекомендации по теме
Комментарии
Автор

Have finished your 2 courses(part 1 and part 2) patiently and that was brilliant. Nice job Chris.

chargeflood
Автор

you are literally the best teacher ever, thank you so much I support your channel and am subscribed with notifications turned on. I will be purchasing your swift courses soon! :)

imladenov
Автор

Oh, it would also be a good tip to show how to break an infinite loop in the playground while it is running. It wasn't immediately obvious that you can type "break" in the loop while it is going to stop it. I find it useful and fun to pause the lesson just after you describe what you are going to do and tried it on my own and then see how close I get.

dcskinner
Автор

Thank you Chris I am learning a lot with your videos.

ThePepefl
Автор

Great Lessons I really enjoyed these videos! Awesome Job!

christopherjschultz
Автор

Having lots of fun learning Swift and using the Xcode IDE!
Regarding the randomization of the array at time 19:42, like @Dennis_Skinner mentioned the loop goes 15 times to fill 10 random numbers (some iterations produce duplicates and are "extra" iterations). I tried a way to guarantee only 10 iterations for 10 items (and also learned how to identify an object type and create an array of some unknown type of another object). Here is code for Swift 3.0.2 (and probably higher):

// Some pre-populated array of any type (this examples is [String]...
var prePopListArray = ["Albert", "Bob", "Camille", "Donald", "Eugene"]
print("Pre-populated array items: \(prePopListArray)")


// Creates an array of the same data type as the prePopListArray...
var randArray =


/* Loops through and grabs a random item out of the prePopListArray and adds it to the randArray. Then deletes that item out of the prePopListArray (so not to have duplicates) and grabs another random from the remaining in prePopListArray. This guarantees to loop only as many times as the size of your prePopListArray. Works for an array of any data type.
*/
while prePopListArray.count > 0 {
let randNum =

prePopListArray.remove(at: randNum)
}
print("Randomized array items: \(randArray)")

Time to play in the next lesson!

MorganMath
Автор

20:11 lagged the fuck out of my computer because apparently, it needs to try over 15, 000 times. FIX IT APPLE. Also good stuff bro its been hard to convince myself to do this and you have made it interesting

hudsoncurren
Автор

Lots of good info. FYI, it says how many times the loop ran in the right side of the Playground. Thanks, again!

dcskinner
Автор

Nice. Thanks again....waiting for next.

allin
Автор

FYI: "repeat {} while count < 10" will give a different result than "while count < 10 {}". 
The first will give 10 values. The second one with the while first will give only 9 values. 
Try it in Playground to verify.

chocoHB
Автор

Hi All. I cant seem to get rid of this error. I'm pretty sure I have it typed out exactly how it is in the lesson. Maybe my brain is starting to get hazy! Can anyone see a problem?

error is "Argument passed to call that takes no arguments"



//Fill an array with 10 ransom numbers in the range of 1-10. Duplicates are OK


for _ in 1...10 {

let randomNumber = arc4random(10) + 1

// array += [randomNumber]

print(randomNumber)

}

brandonmclaughlin
Автор

Nice stuff...! Weird, I just saw a blue screen 'while' expecting to see some text about "while" loops... (reminiscent of Windows, heh heh...)

michaelandersen
Автор

I'm assuming Xcode already knows how to identify animals from a database in a super class? Otherwise, how does it know?

dwinosam
Автор

This module 2 was more complex as i thought.. I hope i can remember this all .. But thanks again

TheCSteve
Автор

Love your stuff man! I know Java so I ran through your videos to teach myself Swift. I have one question? Where do I write my classes in Xcode? AppDelegate, RootViewController, DataViewController, or ModelController? Thank you!

eliseerickson