Learning to code is hard but the first thing you need to get right is to learn the fundamentals.
That is why today I'm going to show you 5 tips so you can become a master of writing For Loops.
This video is sponsored by ZegoCloud. Zegocloud social solutions, help you build one-on-one and
group video calls. Live audio rooms and social live streaming into your apps to help your users
connect and have fun together. Their education solutions help you build virtual classrooms of
all types and all sizes by adding collaborative whiteboards that work perfectly in sync with live
audio and video, screen sharing and live sharing, you can make your online education platform more
engaging and effective. Zegocloud also provides live shopping solutions that can help brands and
merchants bring in-store shopping experiences online. For example in a shoppable live streams
hosts can showcase a product for modeling it and demonstrating how it works in front of the camera
and engage with the viewers by answering their questions and guiding them through the buying
process. But there's more Zegocloud's fitness solutions enable fitness businesses to build
online gyms and provide different types of online fitness services including virtual fitness classes,
live streaming fitness sessions and one-on-one private fitness coaching workouts together
and much, much more. Zegocloud is also great for collaboration. They provide remote collaboration
solutions for building productive remote workforces enabling themes of all sizes to work
from home or anywhere and collaborate effectively with each other by using voice and video calls,
real-time messaging, screen sharing, file sharing whiteboards and other tools. Integration is as
simple as it gets. Get started with four lines of code and embed engaging real-time video voice and
in-app chat experiences into your apps. Click the link below in the description. Before we get into
the 5 tips let's just remind ourselves how we can write for Loops in Swift. So what I have
created here is just a brand new SwiftUI project and what I'm going to add instead of this image
and the text I'm going to add a simple a button so we can basically call that For Loop. So we're going
to run our For Loop right over there and for the action, well actually auto completion is
failing right over here but basically that's it. I'm going to create tip number 0 and
tip number 0 will be basically a reminder of how For Loops can be created. So we are going to
create a function here. Func tip 0 and I'm going to go through all of the ways that you can create
For Loops inside a swift. So basically you need to know the syntax. So I'm going to go through
the first one will be for i in 0 and teal, so two dots and then we need to have an
array that we can iterate a true. So for that I'm going to create the private state variable called
names and it will be just a simple string array of Ada, Bob and maybe let's just add also Claire,
there we go. So we are going to go and iterate from zero till the count of names minus one.
So there we go, we just select count right over there and then we want to grab our current
name. So let name equals names at index i and I'm going to just simply print it out. So let's
just print out name, okay. Let's build it run and see how this looks like. Basically when I tap on
that button we should just be printed out in the console just Ada, Bob and Claire. So let's just wait
for this. Let's tap on the run For Loop button. Ada, Bob and Claire, there we go. So that's one way
of declaring a For Loop but that's kind of the oldest way of doing this. Let's create another
For Loop in our case we are going to use a for name in names and as you can see Xcode 14 is
kindly giving us this code snippet so it's just adding it there. So we just go print out our name
and build and run again so we should see the same output in the console log around For Loop. Ada, Bob
and Claire. That's really nice, but finally I want to show you my favorite one and that is names dot
for each, for each and we are going to get back a name in and let's just print our name. So yeah of
course the output will be the same. So that's kind of a revision of how we could just write For Loops.
Now it's time for the 5 tips that will make you master in writing For Loops. So here we go tip
number 1 is to use break whenever you need it. So yeah take a break but in this time we are going to
take a break inside a For Loop. So let's create the function tip one and I'm going to change
it right over here. So the button will run that function as its action and what I'm going to do is
basically iterate through the array of the names array and I want to see when we find Bob basically
the second one not the last one, I want to break. I don't want to move forward to any other elements
like Claire in this case. So what I'm going to do is just again use the for name in names and first of
all let's print out our name and then if the name equals Bob then I'm going to print out breaking
because we are going to use the break keyword. So we are not going to move forward in the iteration
of the For Loop. So there we go Ada, Bob and braking. It's really nice. Now tip number 2.
If you have 2D arrays please do use nested For Loops. So here we go, we are going to
create, first of all let's create our 2D array. So private state and let's see 2D
array and again I'm going to go with the names. Let's have our first one with Ada and then Bob.
There we go, we need this to be string values and then let's have another array and then Claire
and then and Dan and then Dan pun intended, okay. So we have our 2D array and now it's time
to create our function that is tip number two. Let's just add that also onto our action.
So what we need to do is just iterate through the 2D array and print out all of the names. So let's just
use here my favorite one. So 2D array for each we are going to have an array of names. So again
I'm going to go through that array with another for each and this time around it will be name
and let's just print it out. So print out name. Let's build and run with Command + R and now we
should see Ada, Bob, Claire and Dan. So that is tip number 2, 2D arrays with nested For Loops.
Now tip number 3 is to use map with arrays. So let's just create our function right
over here. Tip 3, there we go and what I'm going to do is just map basically this array names.
I'm going to grab all of these names and I'm going to create a new array with their first character. So A
B and C in our case. So let's see, that will be tip number 3 and let's see. So let's create our
array of characters and I will name this first letters. First letters, there we go and we are
going to grab our names array and let's just map and we are going to grab our each and every name
and we are going to get back our name DOT first. That's a character and finally we are going to
print out our first letters. And if you were paying attention you will definitely see that these first
characters are optional. So if I tap on run For Loop as you can see it's optional A, optional B
and optional C because yeah maybe that string could be an empty string. So they will not be a
first character, okay. Tip number 4. Filter. So you can filter these arrays and what I'm going to do
is just grab all of the elements in this array that start with the character B. So it should be
an array of Bob, okay. So let's create that. First of all I'm going to add in here tip number 4
and let's create that function. So we don't have any errors. Func and that will be tip 4. Not
two functions, one function and that is called tip 4. So let names starting with B, this will be an array.
So we are going to get only one element but still it's going to be an array. So name starting
with B that names dot filter. So filter which one, let's just type out filter. Yes! This is the one and
then we are going to grab our name and the value right over here is name dot first equals our
character of B. So now we just simply print out our names starting with B. So again let's build
and run and let's see if we get this Bob and there we go. We have an array and it has only one element.
Okay! So final tip. Tip number 5 is sorting. So for the sorting I want to first of all create
an unsorted array and let that be again another private state variable, unsorted names.
And let this be maybe Bob and then Claire and then Ada, okay. So this is an unsorted names
list according to their first character of course. So what I'm going to create is a function called
tip 5 and that will sort our array. So let's just have that on the button. So let sorted names
equals, we are grabbing the unsorted names and then go sorted by and we're going to have
here two elements. That's name 0 and name 1. And what I want to do is just go name 0 is
less than name 1, okay. Finally let's just print out the sorted names, okay. Let's build
and run and see this in action. So we should get that sorted Ada, Bob and Claire. So it's
really nice. Now you are a power user of For Loops in Swift. And if you'd want to know how
I learned how to code for hours on end go ahead check out this video right over here.