Full transcript of “Swift Programming Tutorial – Full Course for Beginners”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 Swift is a popular programming language often used for creating iOS apps this is the perfect course for learning Swift vandad teaches this course he currently works as a lead iOS Developer and he's created many popular courses hi everyone and welcome to this course my name is vandon and I'm a senior full stack developer from Sweden in this course we're going to talk about Swift as a programming language without blending in Mac OS or iOS or back-end development we're going to talk about variables constants structures enums classes you name it even modern futures of Swift as a program language such as async and await I've been doing iOS development myself since late 2007 and I've been using Swift since 2014 when it
0:46 came out first publicly this course is built for people who are already familiar with another program language from before you don't have to be very proficient in that language that you're using but as long as you know the basics it could be python typescript JavaScript rust whatever it is but as long as you're familiar with the basics then you should be good to go this video itself is part of a bigger playlist which is available on my YouTube channel and which you can find just by searching for my name and this playlist which I'm referring to is called Full stack with Swift and the goal of the playlist is just to get us familiar with doing Swift development for both front and and back end so if throughout this course I sometimes make references to this playlist then at least you know what that is for before we get started I'd really also
1:32 appreciate it if you could give me a follow on YouTube just by searching for my name the link to which you can find in the description at the bottom of this video and I'm also very active on Twitter and Linkedin daily by publishing tips and tricks about programming languages and just generally informing the public about languages such as python typescript JavaScript rust Swift you name it so without further Ado let's just get started with the course hi everyone and welcome to this chapter of full stack with Swift in this chapter we're going to look at all the modern Concepts that are available in Swift as a programming language if you've already watched an introduction you probably know that and that this course generally
2:17 not just this chapter but this course generally is not for absolute beginners so I expect you to already know another programming language it doesn't it doesn't really matter what it is it can be python rust it can be typescript JavaScript whatever as long as you know the basics of programming languages such as what functions and variables are then you're good to go if you don't have that experience from before this course generally might be a little bit difficult for you to follow through with and I will do my best to explain everything but since I I don't really want to explain all the basics um I think it is best that you perhaps have a look at some guides on getting started with another broken language such as Python and then get started with this course however if you haven't worked with any other program language
3:03 from before and you're also very talented as well and which I assume every everybody is you might be able to follow along with this course without problem so what we're gonna do now is to go ahead and set up a playground basically a workspace with many playgrounds in it and playgrounds are Swift's concept of being immediate being able to immediately see what your changes are doing and it will allow you to basically run your code automatically as soon as you make any changes to your code so it is a very powerful way of testing code or even creating some projects in it you can you can create a whole project with a playground and see the results graphically even on the screen
3:49 so it's very very um powerful for those of you who are familiar for instance with flutter and dart you already know that you can see your changes and display it on the screen immediately and playgrounds are no different they're very similar actually to how Dart and flutter work but a little bit actually simpler I would say because we're writing pure code and just seeing pure text displayed on the screen as a result of for instance calling a function so in order to continue with this chapter you probably need a a Macintosh um however what we're going to do we're just going to use Swift foundation and SDK in here or a library so you can actually follow through as well on Linux
4:35 such as if you have for instance Visual Studio code re setup and if in and you've installed Swift you should be able to follow through the only difference will be that those of you who have a Macintosh and xcode installed you will be able to create a simple playground and see your changes immediately for those of you who are following this on Linux and visual studio code for instance you need to run the code manually so you will need to just type your code and then call Swift compiler from from your terminal and we're a console in order to run those codes so it shouldn't be that different so you should be able to follow along even on a Linux machine so without further Ado let's just go ahead and create our workspace now I'll explain
5:22 what a workspace really is a workspace is a collection of pro projects you could say a collection of files and projects it's it's really just one file that explains the structure of a project to xcode if you're if you're running things on Linux so you don't have to create a workspace you probably can't because xcode does that and unfortunately this xcode um obviously xcode application creates like a proprietary format for all of its files kind of so not the text files but like the workspace and project files they are proprietary XML format which is puzzling Y in this day and age it has to use XML really but um that's a different subject so if you're
6:09 following through and with the course at home with Linux then you don't have to create this workspace but if you're at home with a Macintosh you need to go ahead and create this workspace together with me so I'm going to bring up my extra just enough in here so that we can see what we're doing I'm going to go to new and let's just say workspace then in here let's just say Swift and crash course YouTube okay in here and let's just say save this workspace as you can see it's just an empty workspace at the moment so it doesn't have any contents what we're going to do then let's just increase the size of this so it takes the entire screen and then we're going to go ahead in file new and then we're going to go into a
6:55 file and in here type playground in the search field and add a new blank playground okay so the first concept that we're going to talk about in here is going to be called variables so let's just say variables and I can see that our workspace was actually just creating here without any folder so we might be able to remedy that before we click create our playground let me just go ahead and open a finder window in here so let's just go ahead in here you don't see actually what I'm doing but right here is the folder then I'm going to take a I'm going to create a folder in here let's let's say Swift crash course YouTube
7:40 and place this and and then we can close our xcode place the workspace in there and let's just double click on the workspace to open our xcode so now all the playgrounds that we're going to create are going to be creating inside that folder okay so let's go ahead and create our variables for a playground from scratch so file new file okay and then in here we're going to say playground and you can actually find playground a little bit lower in here I think as somewhere in here you can see in the playground section so let's say blank playground and let's call the variables okay because we're going to talk about variables now so this is the default thing that you can see in here and basically this is your view you might see A Whiter screen
8:25 in here simply because well maybe you haven't changed your themes in xcode and that's something that you can figure out on your own how to change themes and exit we're not going to talk about that okay let's clear this and instead of importing UI kit we're going to just import foundation and if you're watching this course at home or wherever you're watching this on Linux for instance then and if you don't have a Mac OS you need to also do the same thing just import Foundation just create a simple Swift file you don't have to create a playground just click create a plain text Dot Swift file and then import Foundation library in here okay foreign foundation and Foundation includes all the basic as its name indicates it includes all the basics of what Swift contains really like how strings work
9:12 how integers work all the default operations between these different variables and even optionals Etc so importing foundations is one of the most important uh things you can do basically in your Swift file as you're as you're getting to learn Swift okay so let's go ahead and talk about variables in Swift There are two different syntaxes or keywords for creating variables One is using a let and the other is creating a variable using VAR so let and VAR the difference between these is very confusing for some people and that they think that let creates immutability and VAR creates a mutability
9:57 however the only thing different between these two is that a let variable cannot be assigned to again whereas a VAR can be assigned to again and there are some other small differences between them such I mean we will actually look at those things and when we're working with classes for instance or structures how they differ from each other and but for now just know that let's cannot be assigned to again and far can be assigned to a game but also keep in mind that there are some other small differences between them which we'll explain okay so let's go ahead in here and say let's my name and then we say equal to Lambda for instance and then we say let your name is Foo okay after you've done these and
10:44 created these two variables you cannot assign to them again simply because there are less variables so let's say my name is your name and if we go down here at the bottom of the screen you can see my mouse and I'm going to hold on this button so I'm just going to press my track path and then say automatically run and this will just ensure that this playground will be automatically executed anytime we change anything in it okay you can see the error it says that cannot assign the value my name is a let constant so that's how you know that you cannot assign to it again after creating a let constant you just need to leave it you don't have to you you can't basically assign a new value to it okay so that's how let's works now let's go
11:32 ahead and create a variable so for now we're just going to create a simple array and already assume that you know what arrays are but we're going to talk about collections a little bit later just know that in Swift you create an array with this syntax with square brackets okay so let's say VAR names is we're going to say Mining and your name okay so we just created an array in here and you can then see the results to the right hand side you can see this array contains van Dot and a foo okay now you can go ahead then and say since this is a variable then it can be mutated even internally if it contains for instance a structure so we say names and then we can call an append function on it and we say bar and then we can
12:18 also add baths to it as you can see in here so by the end of this then our array called names contains found.foobar and bass however if you change this to a let you will see that we'll get some errors in here saying that this is a let constant cannot use mutating member on immutable value names now this goes to this goes a little bit deeper in how let's mvar are different from each other but in order to understand that we need to understand what structures are because arrays in Swift by default are structures if you're not using old Objective C arrays for instance so just know that by creating a variable array you can append to it and you can remove
13:04 stuff from it for instance as we'll see later however if this is let you cannot mutate even the internal values inside it so you can see there are two mutability concepts in in Swift one is that you can actually mutate this array for instance you can say names is completely equal to another variable like the law okay so you can see all of a sudden it became blah blah bar bass you see so this is mutating the variable itself it is actually assigned a complete new array to names as a variable and these two statements are mutating what is inside this variable so doing a let in here prevents both assigning a new value to names and also mutating names
13:52 however having a VAR you can assign a new variable two names and you can also mutate names internally okay so this is another difference between let and VAR so it is also very very important to understand these two differences mutation of the variable internally and assigning a new value to the variable to mutate it so a lot of developers struggle with that especially in the beginning so I completely understand if it is a little bit difficult to start with but the earlier you learn this concept the better because otherwise I think a swift can become quite difficult for for you to understand going forward so spend some time and experiment with a
14:39 let's and VAR but we will have quite a lot of examples in this chapter so you don't have to do it on your own okay so now if you change a variable like if you've created a variable and you assign a new value to it and it won't it won't change the object that it originally was assigned to now I will explain what that actually means so let's say let's Foo let's create another space in here let's say Foo is foop and then we say VAR Foo 2 is equal to foop like this okay you can see now this variable of food 2 contains the value inside Foo all right and you can see both of them contain the string Foo now if you go ahead and set
15:24 the value of Foo 2 to for instance Foo 2 like this you can see that Foo and futu still contain the values that you'd expect Foo contains food and food 2 contains the new value so by assigning a new value to Food 2 which was previously equal to Foo you're not overwriting the value that was inside Foo simply because this is referring to this and the reasoning behind this is that we're working with something called value types now I will explain more value types later but all you need to know for now is that by assigning the value of Foo to Foo to we're simply copying this value over to futu
16:09 so this value is being completely copied and you're creating a new instance of it inside food too so now we have two Foos inside memory by this point so we're not getting a reference so it's not a reference type that we're talking about Swift has value types and reference reference types for those of you who are not familiar with this concept from before don't worry about it I'll explain it more in details but for those of you who are familiar with this concept just know that structures which are basic basically as Swift string is a type of structure it in structures are basically value types and then we also have classes and which are reference types and we'll explain all of these later in the future but just know that by assigning a value
16:56 that was assigned to a variable would let assigning it to a variable like this with VAR and then changing the value of that variable and assigning a new value to that variable you're not changing the value that was assigned to that variable initially I know that I mean this the sentence is a little bit complicated but I think I think you get my points in here what I'm trying to say okay now we can have a look at another example of this and how that looks so let's go ahead and say let's more names and let's create an array in here and then we say Foo and then a bar okay then let's let's say we create a copy of it we say a copy is more names and
17:41 simply because this is a let's variable and also that we're working with a swift array which is a structure then we should know that structures are value types and assigning the value of a structure to another let's variable in here creates a copy of this entire array for us okay so now that we have sorry let's make this a copy variable basically let's now go and add some value to this cop a copied variable and say append and we say Bas all right and then we say let's get the value of more names and then the copy you can see more names in here still contains Foo and bar which were the original values however they copied the variable now contains
18:29 bass as you can see in here so adding Beth to this copy did not change the values that were inside more names and this again goes back to how swift is working with value types in here it is copying over the contents of more names into this copy variable and changing this variable's internal data does not affect the original data which it was assigned to in the beginning okay so hopefully that makes sense by now now one thing that can be a little bit confusing for developers is when when they're working with let M VAR but with old Objective C classes all right and this this can be a little bit strange to
19:15 start with like a lot of developers who are not so familiar with swift would say that let creates immutable variables so you cannot change them internally however if you're working with a let's variable with a let constant for instance in Swift however that that its internal value is an instance of a class that class can mutate internally without this letter variable getting in the way let's have a look at an example for those of you who are familiar with Objective C you know that there is a class inside Swift which is called NS mutable array now if you just want to learn Swift you don't at all need to know about this it is
20:00 simply for those of you who are a little bit curious about let's and VAR okay I'm just going to demonstrate this to you but please if you're not completely understanding this example it is completely fine chances are if you're working with Swift and you're learning Swift swift UI and even if you're working with UI kit for instance you create your UI applications for iOS Mac OS whatever chances are you may not really be using the this uh thing that I'm gonna show you at all or maybe you'll use it once or twice in your entire career and more and more Swift gets modern less and less we're going to use these classes so but for those of you who are Mac Developers for instance you probably actually work with NS Mutual array perhaps often okay if you especially if you have an old code base
20:46 so let's create a constant in here where we say let's old array and we create an NS mutable array in here okay and then in here we say that the value is an array okay and then we say the array contains the values of Foo and bar like this okay then you can see in here that Swift playground says old array as Foo and bar right and you see oh this is let so we cannot change its internal values because if you for instance have a look at this one an array again okay which is created with let if you say more names and you say append and you say hello you will see that you will get an error in here it will tell you that you cannot append to this variable
21:32 because it's created with a let statement or a let's syntax okay over in here since we're creating a let's old array that is equal to a class instance which is not a structure and again I completely understand that we haven't talked about classes and structures yet but just know that this is not and this is basic as its name indicates it is mutable internally it can be changed whether it is assigned to a let or a VAR it doesn't make any difference it is a class whereas this is a structure okay and structures are value types and classes are reference types okay so just know that for now that this is indeed mutable even if it is assigned to LED variable okay so now all of a sudden you can say old array and then you can say add and let's say baz
22:19 all right and now you can see Swift playgrounds runs this code and tells you that indeed your let's constant what you thought was constant has changed internally all right now you can go ahead and create a new verbal and we say new array is old array and if you say new array add q u x in here Cox okay and then we say old array and we say new array now you can see the results in here since new array got the value of old array you add a new value to this new array and you'd expect probably for new array only to change not for old array to be affected however you can see the result that both old array and new array are actually referring to the exact same array so this can cause a lot of
23:05 confusion for developers who are for instance new to Swift thinking that oh I just created a let immutable variable no one can change this however you can see that it got changed and even worse you can as if if you pass this value for instance to a function and that function itself can also change this variable so it is quite dangerous just by using let it's not that we're completely getting rid of mutability in Swift so a lot of things can go wrong especially if you're using reference reference types such as nsmutable array but don't worry about it I mean if if this is a little bit alien at the moment I will explain this a lot more in details later okay as I mentioned be very careful when you're working with mutable types in
23:50 Swift and when you're passing them for instance to functions because those functions can actually change your mutable types even if they say they don't okay so let's have a look at this let's say we create another array in here let's let's copy this and I'm going to say let's some names and then we say Foo and bar okay so that's fine let's create a function and I completely understand we haven't talked about functions yet but this is this syntax of what functions look like in Swift so we say funk change the array and then we have an argument call array and in here we say NS array okay now I'll explain to you what nsra is nsra is the um and it's mutable array is the mutable
24:37 variant of nsra and if you actually go to the source code for NS mutable array let's say in its Mutual array you can see that it's subclasses nsra so anywhere you have an NS Mutual array and sorry anywhere your function or call expect and NS array you can actually pass an instance of NS Mutual array to its because it will just work it will be downgraded to ns3 okay so let's say that we look at this function and this function just looks like it is working with an immutable array in here however internally it can say let's let's just say copy as array as in its mutable array so this is a syntax in Swift that you shouldn't really use that often but code can actually do that without without getting
25:24 bugged by Swift really okay so you can see in here we're saying we're creating a copy uh oh a rage array like this where we're basically creating a variable that is called copy however it's not really copying the instance it's just named copy so maybe we could just call it array2 or something or just copy but uh just know that it is really not copying the value inside this or it is just assigning it to this variable and but it is setting its data type it's promoting its data type to immutable data type it's assuming that it is mutable okay it's this is really bad code you should never write stuff like this okay but I'm just demonstrating that it is possible to write code like this and you should
26:10 kind of guard yourself against code like this and then in here we say copy dot add Mass okay now if you pass if you call this function change the array with your Mutual array everything will just work as expected some names and then you can say some names and have a look at the data now when this code is run you can see in this scope some names after being passed to change the array has actually been internally mutated you can see Foo bar and pass so this pass that this function added to this array is actually present outside that function as well simply because we're working again with reference types meaning that there's one copy of this in the memory and this function is actually changing the original copy okay
26:56 so that was really uh it that's what I wanted to explain in this particular section of the video with variables and uh constants so just know that you have let and you have VAR and let it if working with value types such as structures of Swift arrays or strings or integers it prevents immutability of that value internally meaning that you can't change this van dot to for instance say and my name my name uh and then you can say is equal to actually my name make uppercase you can't do stuff like this so you cannot and like change the value that is assigned to this and you can't
27:43 reassign a value to my name after it has been created okay so the reassigning that you cannot reassign a value to a let constant applies to both reference types and and value type so it doesn't matter if if the value inside this let constant is a string or it's an instance of a class as long as it's let you can't assign a new value to it for instance let's say here is you can see it is a sum names it is an instance of a class which is a reference type However the fact that you cannot reassign a value to this still holds true whether it is holding an instance of a class or an instance of a structure so let's say some names is blah in here now we should
28:31 get an error in here saying that you cannot reassign another value to this after you have created it so this reassigning to a let constant applies both to reference types and value types however the internal mutability of a let's constant only applies the prevention of internal mutability of a let constant only applies to Value types such as structures if you've assigned a class to a let constant the class might internally change without Swift actually complaining at all okay so I completely understand is a little bit complicated and I personally believe that some other personal languages have implemented this a little bit more beautifully and a
29:16 little more like fluid such as rust can I have some courses actually about rust as well so you can watch my Ross crash course for instance on YouTube and learn about mutability and rust as well but I I believe that Russ has implemented mutably a bit nicer than Swift and because Swift can cause some confusion in how it handles and mutable values but for now just know these simple rules and then we can move on to the next section of this video all right now that we've talked enough about variables let's move on to another concept which is very very important to learn in any programming language that you're learning at the moment and those are or that is operators so operators are small functions that are a little
30:02 bit special in how they're declared and Swift treats them as such it treats them a little bit more special than other functions so and let's have a look at what operators are but before doing that let's create a simple playground for operators so let's say go let's say file new and we say file if we can find it oh we can't because we can't create a file inside this so let's be in here inside our editor and I'm going to say command n and then I'm going to go ahead and create a playground in here let's say blank playground and then we're going to say operators but be very careful because by default so if playgrounds or xcode actually tries to create this playground inside another playground and that is horrible it is really off full and just please just use the main folder
30:50 to create your playground and also add it to the main xcode workspace structure as well in here so let's say create now we have operators and let's do the same thing in here I want to say import Foundation okay after doing this let's start talking about operators so let's create two constants in here let's say my age and we say 22 and we say let your age is 20 okay now let's work with our first two operators and those are the greater than and less than operators if you have two integers like this and I kind of expect you to already know what an integer is from other program language perhaps that you already know then we say if my age then once you have two integers you can
31:35 use this particular operator that you can see it says greater than so the left side should be greater than the right side your age and your age and this is an if statement okay which I kind of already actually I kind of expect you to already know what if statements are so you can see now we're using an if statement and then and putting our left hand side and right hand side variables in here and using operator which is having this kind of a syntax so what this operator is doing is comparing the value to the left with the value to the right and it Returns the value of true if the value to the left is indeed greater than the value to the right okay and here then we can say I'm older than you okay and then we say else if my H is
32:25 less than your H so this is another operator that works between integers for instance that says it will I will return true if the value to the left is less than the value to the right then in here we say I'm younger than you and else oh hey we are the same age okay so if I'm not older than you and I'm not younger than you then we should be the same age logically all right you can see nothing is happening in this playground because we don't have automatic running so I'm just gonna hold my trackpad on this button and then I'm gonna say automatically run and then you can see that our code is going to run in here and the value basically we got into this branch of our if statement I'm older than you simply because this operator indicated to Swift that the value to the
33:11 left is indeed uh larger than the value to the right return the value of true and then Swift fell into this if statement so that's these two are the first operators that we're seeing in Swift basically however I mean there are some other very special operators such as the equal operator that we're seeing in here but we shouldn't really go ahead and explaining that we're simply assigning a value to a variable in here with that operator okay now there are other operators such as Plus for instance let's say let my mother's age is my age plus 30. so this is a an operator that is operating between two integers I can from the left hand side and a right hand side so it simply takes the value to the left and adds the value
33:56 to the right to it and then returns that result to us okay so that's another operator that we're seeing in here the plus operator you can also of course use for instance the multiplication operator and we can say let's double and double my age is my H and you say times two and as you can see this is an operator that is sitting between two integers again it takes the integer to the left and and multiplies it by the integer to the right and Returns the value and which then gets assigned to this variable you can see it's 44. if you remember the age was set to 22 times 2 is 44. so you've probably now seen a few operators in here and you're wondering
34:42 okay how many different types of operators are there and there are three different types of operators in Swift and I mean not I'm not counting these operators I'm just talking about three different types of operators okay the first one is called a unary prefix the second one is called a unary postfix and the third one is called a binary infix okay so when we're talking about unary as you remember let's actually write them down let's just say uh one is unary urinary prefix the second one is urinary postfix and then the third one is binary infix okay
35:27 wherever you're seeing unary just know that this thing is working with only one value again working with one value and binary is working with two values okay so you can now imagine that this operator the multiplication operator that is working with two values is indeed a binary in fix it is binary because it's working with two values the left and the right hand side and it is an infix operator because it's sitting in between so infix in between okay it's sitting in between two other values so all the operators that we've seen so far the greater than or less than basically addition and multiple multiplication are binary infix operators we haven't seen a unary and Sr unary prefix and unitary postfix so
36:14 let's have a look at unary prefix as you'd expect unary Works only with one value so let's say let's Foo is true so this is just assigning the value of true to Foo however if you want to change the value of this true to false and you want to flip it basically you use this operator in here which is the apostrophe operator which is a unary prefix operator that takes the value to its right hand side and basically um negates it okay so you can see in here true became false simply like that so this is a unary prefix it's prefix because it's before the value that comes right after it okay then we have a urinary post fix so a
37:00 unary postfix offer is a little bit special I mean in all my years of working with swift I've been working with swift since summer of 2014. it is very very seldom that you create your own post-fix operator and you want to read postfix There are some unitary postfix operators already it's built into Swift but and you use them perhaps quite a lot however creating your own postfix uh unary postsex operator is something that you do very very seldom okay let's have a look at a unary postfix operator let's say name and I'm just going to create an optional value now I know we haven't talked about optionals but just for the purpose of demonstrating what a unary postfix operator is let's go ahead and write
37:45 this syntax let's say optional and then we say rendered okay now if you want to grab the value that is inside this variable and unwrap it we say let unary postfix is equal to name and then we put this apostrophe after this name now just know for now that what the what we did in here is that we put an optional value into a variable call name an optional simply means that this name can either contain null values or it can contain a value that's what optionality in Swift means but I've prepared a completely separate playground for optionals which we're going to talk about a little bit later however this you can see now it goes ahead into this optional value and grabs the internal value and assigns it
38:31 to this okay so let's say if we inherit say type of name and then we can type of unary postfix now they have two different types you can see the type of this variable is an optional string and the type of this variable is indeed a string so this unary postfix operator went inside this optional and grabbed its value out and forced it out and assign it to the variable to the left hand side basically using the assign assignments operator so this is a unary postfix operator it comes after a variable and it works only with one variable so it doesn't have a variable here and a variable there it is only working with the Val and the
39:16 variable to its left hand side okay prefix unary prefix works with the value after it post or postfix works with the value before it okay so that's the second example basically for unary postfix now for the last type of operator which is the most common type and we call them binary infix simply because they work with two values hence the name binary and also infix simply because they sit in between the values that they work on okay so let's say a result is one plus two and this is a simple example of a binary infix operator you can see it's working with two values and sits in between them okay and other another example is let's say
40:03 let names is equal to Foo and then we say plus empty space plus bar and this is another um infix binary operator that is sitting between two strings you can see you may be a little bit confusing you can say oh but if this is sitting between these two strings what is this sitting in between is it a prefix operator well it is not what is happening here is that Swift is executing this code from the left hand side because there is no parenthesis then it says okay Foo plus empty space becomes Foo plus like empty space like this and then takes this plus and uses it in between the result of the previous statement and Bar okay so that's what is happening inside
40:50 this code basically okay so that was an example of a binary infix operator and as I mentioned most operators that you use in Swift are going to be binary and fix there are quite a lot of actually other operators as well but we don't use them so often okay depends again also on your code base now there is another category of operators and which is called a ternary operator okay and ternary is something that is a kind of a Hot Topic in any programming language that supports ternary simply because it can be misused and it can cause your code to look very very difficult to read actually so um what I suggest oops what I think is a good idea is always a check with your uh
41:36 with your teammates who are working with your colleagues basically in your team and ask them if if they're comfortable with ternary and if they're if there are what are the um what are the boundaries that they that they feel comfortable using ternary operators within ternary can become very complicated especially if you're nesting them if you're using a ternary inside iternary and it's just good to check with people what they're comfortable with okay but let's just have a look at a ternary operator let's say let's a H is 30. now let's say that you want to display a message to the user saying that let's message and then you want to say if the age is over or equal to 18
42:22 then this message should say you are an adult however if the age is less than 18 then you should say you are not yet an adult how do you do this well what you could do is to say let's message is a string okay uh and then you could say if H is more than or equal to 18 then message is you are an adult otherwise message as you are not yet an adult so this works actually very well and you can type the message out you can see now it says you are not all simply because the H is 30 or if you were 16 it would say you are not an adult yet or
43:09 you're not yet an adult however there's another way of writing the same exact code so let's just comment this out okay and that is using the ternary operator so in here you can see we've defined our variable first and then we're going using an if statement and an else statement and then assigning values to that variable okay however you can also say let's message is then you would put your condition in here you say condition okay and then the format is then you put a question mark then you say value if condition is met and then so let's put this to the next line and then a colon in here and value if the condition is not met
43:57 so this is the format of a ternary operator first you put your condition in here so what is the condition so if the age is more than or equal to 18 so just let's just write it age should be more than or equal to 18. then we say that the value should be you are an adult then the value if the condition is not bad is you are not yet an adult and that is exactly how you write a ternary operator so you say you take your variable that you want to actually calculate the value of then you say it's equal to then you put the condition of your ternary operator and then a question mark after a question mark follows the value that you want to assign to your variable should the condition be met and then a
44:43 colon and the value that you want to assign to the variable should the condition not have been met okay and you can see now the messages you are an adult and if you change this value again to 17 you can see the value will be changed to you're not yet an adult okay so I hope that you've now got an idea of some operators in Swift you've we've had a look at different types of operators in unary prefix unary postfix we have binary infix and we've also looked at a fourth category of operators which are ternary operators and again just be a little bit careful with ternary operators because not a lot of developers are actually very comfortable using these I met I personally like
45:28 ternary especially like in these cases where is less code is understandable it's compact is not so much logic however some developers just write them all in one line like this and that can be a little bit difficult to read I I actually agree my uh habits in Swift is to manually break my code down into smaller bits and pieces like this into different lines as well unfortunately unlike some other programming languages like rust and dart and there is no internal analyzer as such sorry there are no formatter so Swift Code you need to format it by hand unfortunately and it has been eight years since Swift's come out and
46:13 there is still no internal formatter for Swift of course you can select your code and then indent your code with Ctrl I in xcode but you cannot have xcode format of your code for you as you type so that is a little bit of an unfortunate problem that we have to deal with but it is what it is and we can just get used to it so if you want to make your code a little bit more readable I suggest that you format it a little bit by hand so kind of handcrafted as well okay that was really it for the operators so in the next section we're gonna look at if an else statement so see you there now that we're done with operators let's go ahead and create a new playground or
46:59 if and else so right here I'm going to press command n on MacIntosh and let's say playground in here oops if I can spell it and a blank playground and let's just call it if else and please just ensure that you're not creating it inside an existing playground you can see in here it's called operators and we should change that to be inside our main folder basically okay and then we say it shouldn't be inside the operators group either it should be inside the group op ARP workspace so create now we can collapse the other ones and in here just like normal we say foundation not like normal actually like usual I meant to say so let's talk about if and else
47:45 statements we've already seen a few examples of if and else and if you've already done programming in another programming language you should already be familiar with what if and else statements are so it's just a conditional basically okay let's add a few values in here so I'm just going to copy and paste some values and place them in here so some seed values that we can work with Okay let's create a simple if statement and then if sorry an if an else statement so we say if my name is equal equal valid so in here so like this and so this is using a lowercase v as you can see in here okay then we say your name is and then we're going to add our variable within a string and this is how you do it in so
48:30 if you say back backslash and then parenthesis and then you say my name in here okay then in here we can say else and with no conditions in front of it and then we say oops I guessed I guessed it wrong like this and then let's do an automatic run for our playground and we should now come to the oops part because we guessed the name incorrectly with a lowercase v as you can see in here so this is a simple if statement so you compare something with uh with something else and then you can have an else block or not you can actually just have an if statement as well so you don't have to have an else Block in Swift okay now you're not limited in you're not limited only to one else statement you
49:16 can't have else if as well okay so let's just say if my name my name is equal equal land up with the proper spelling as well with a capital V then we say now I guessed it correctly okay and then we say else if my name is Foo now you can see that we have another Branch so we have an if and then if that case is not met then the program will fall into this statement so it's very important to understand that it's not that Swift is executing both of these concurrently it's executing the first one first if that condition is met then Swift goes in here and ignores everything all the other statements inside the if statements so all the
50:02 other else ifs and the last L statement if it is available so it's just branching basically okay so if my name is Foo as we say are you Foo and this doesn't really matter what you type in here I'm just trying to demonstrate how if and else if basically statements work in Swift and then we can have a final else statement in here and says okay I give up okay and you can see that Swift has gone into this Branch because name was correctly set to Van Dot and we're comparing it with vandan sorry it wasn't set to Vanda I mean it was set in here but we're actually comparing it correctly with the correct value in here okay and some people write their if statements a little bit more in like what I call a flipped way and here you can see we're
50:49 saying if my name is equal to Vandal and so if you're reading it like you would read an English sentence I would say if my name is equal to valid however some people actually do it in the opposite way like this they would say if van dot is equal to mining so this is another way of writing it an uncommon way of doing this and a lot of old and like a little bit old school developers would do stuff like this it is not so common in all my years of working as a software engineer I've seen one or two people preferring this however me and my colleagues have been able to convince them otherwise that this is this we shouldn't do this in most modern Source codes don't do this however you may be an exception in so maybe you're
51:36 working at a company where this is the prevalent way of doing if statements in that you actually put the value and that you and are testing um last and then you compare with the value and that you're expecting first as I said this is a little bit uncommon in my experience but maybe at the company that you are you and your teammates have decided that this is the way to do it it is completely equal to this it is the same thing as this code and I mean what it achieves is the same however the syntax is a little bit different okay so it is up to you and your teammates to decide what which way to do however if you're new to Swift I beg you to please don't do this and it is this way
52:24 that you should learn how to write your if statements so please don't flip these values however if you're a seasoned developer and you know exactly what you're doing and you and your teammates have decided that in your code base all your if statements should be written like this or in just a few exception cases you write your code like this as long as you've checked with your team is and everything is fine then you go ahead and do it but if you're learning Swift just now this is the way to write an if statement okay and this is the way that almost everyone writes their if statements seasoned or not seasoned good so that was simple these are some simple if statements basically comparing a value with another value but what if you are having a little bit more complicated logic in here
53:11 so let's say that you want to make sure that your name is found out and that your age is also 30. so you can see the age is defined in here as 20. let's just go ahead and compare it with 30 instead let's say if my name and in here if you want to compare also the age you can say and my age is 30 for instance okay then we say name is landlord and age is 30. then we say else if a my age is 20 then we say I only guess the age right and then an else statement in here we can say I don't know what I'm doing okay so as you'd expect now we're going to go into this line of code so what's happening here is that Swift started executing this code
53:57 from left to right and I said okay from left the name is truly vandot and it started comparing the age and that says age is indeed 20 is not 30 so I'm not going to go into this if statement so it ignores this if then it looks at the code that follows this if statement and says oh it's an else otherwise if the age is 20 is the age 20 yes it is then it falls into this statement and then ignores the rest of the else so you need to think of if statements as in branches and they get executed separately not concurrently and they're basically in a Serial order from the first statement if it fails goes to the next statement then goes to the next statement if any of these statements
54:44 succeed in trapping the logic so this one trapped and said okay if the age is 20 and in it is indeed then the code goes in here if any of these else statements or if statements do that the entire rest of this code is then ignored so this didn't fall into so this we didn't fall into this we did fall into this and then the rest is completely ignored regardless of how many else's you have in here you can say else if blah blah and then another else after that but all of those will be ignored okay now just like we have the an operator in here the logical and operator we also have the or operator okay but you need to be careful with it because you can
55:29 actually make the code and do things that you didn't want it to do so let me just explain something in here so let's write an example let's say if my H is 20 not 200 or my name is Foo then we say either age is 20 name is Foo or both so what happening here is that you can see we say you may be reading this saying either the age is 20 or the name is Foo but what Swift does reading here says either the name at the age is 20 or the name is who or both of these so even if both of these are true then this code is executed and the reason behind that
56:15 is that if your age is 20 then the code actually comes here just by default it doesn't even execute the rest okay so that is the reason that if age is 20 your name might indeed be Foo and that is what you've written in here but the code has already fallen into this simply because the first part of the condition was met so Swift doesn't even execute this part it says oh ages 20 of course let's go in here okay simply because Swift looks at the statement app versus oh said or so I don't have to execute it simply because I already know that my age is 20 so it falls here okay so it can be either this part this one or both animistics and developers make is they
57:01 think that they can add another else Block in here and that else block will be executed as well and that is not the case because as I've explained it before if statements are zero and they get executed one at a time if any of them traps the um the execution of the program meaning that it actually results to the compiler understanding that oh this branch of code should be executed then the rest will be ignored so if in here you type else if my name is Van dot even if your condition is met which in this case is because my name is truly vandad or my age is 30 I started 20 which it was set up here if you remember my name and my age were van dot and20
57:48 respectively this code will not be executed so let's say it's too late to get in this class you can see that it won't be executed it won't be displayed in here only this branch is executed simply because my age was 20. okay and I can change it actually to Foo and you can see it still gets here first because my age is 20 and the operator after that is a logical or meaning that oh if this or this or both and it's since this one is true it doesn't even have to execute this okay so as I mentioned yeah or can actually or an and logical operators they can get quite complicated in that if you're if you're working with these operators and
58:33 also parenthesis then you could create kind of confusing code as well and if you actually don't use parenthesis sometimes your logic could be completely incorrect let me just show you an example let's write a coding here and we say if my name is landlord and then we go to the next line and we say and my age is 22. okay then we go to the next line and your name is Foo and remember my name is my age is 20. your name is Foo at your age is 19. okay then here we're saying your name is who and we know that that is true in fact or your age
59:21 is 19. okay so we say and what is this code actually doing so we say my name is Found Love and I'm 22 and your name is Foo Foo or you are 19. do you see what happening here so what happened was that and you can see that this message actually is printed here in our playground meaning that the code did fall into this as into this execution Branch so what happened you can say that my name is Vanda and my age is 22 but you can say well your age is not 22. how did this code come here and the reason is because we're mixing ands and ores and you can see what what Swift reads in here says okay if your
1:00:08 name is Van Dot and your age is 22. and your name is Foo is this true then looks at all these ants this is yeah your name is but your age is not 22. then it looks at the rest that it could and says do I have any other logic to follow and it says yes actually you've written it here either these or that your age should be 19 and it looks at your age so uh Yes actually your age is 19. so you can see how this could be confusing for you reading this you may think oh your name should be vandot and your age should be 22 and your name should be Foo or your age should be 90 so you may think that this part is executed separately and this part is separate but since there's no
1:00:54 parenthesis between these Swift reads it from left to right and then there's no uh precedence between the ands and ores so they have the same precedence so it says okay this entire thing is the same precedence and I just execute it from left to right and this or at the end basically traps the execution branch and ensures that the code falls into the if statement okay so writing code like this could be a little bit confusing what you might have wanted to write is this instead you probably wanted to say if and then parenthesis okay and then this is your own logic so you want to basically check your name and age so let's put it inside parenthesis so we say my name is founded
1:01:40 and let's go to the next slide and my age should be 22 so you're basically saying and so you say my name is Vandal and my http 22. then you go to the next line and you say and and another parenthesis your name is Foo or your age is 19. like this okay so what is happening in here is uh and I can see our playground has some problem so what this really equates to I mean if you want to read it in English you could say my name is founded and I'm 22. and your name is Foo
1:02:28 or you are 19. so do you see how this is differences your name and my name is Van Dot and I am 22 so this condition should be met and so should this because we are using an and meaning that not only this condition should be met but also this condition then it says okay well this condition is met because your name is Vandal and your age is 20 actually oops we have 20 here so that's that's interesting so so we wrote 22 that's fine I mean let's let it actually be like this so it says my name is Vandal and my age is 22 so it says oh this is not met okay so as soon as this is not met and this is an and statement it says oh I don't have to execute any of this now because I know that the left hand
1:03:14 side of the and operator and The Logical operator is not exec is not true so I'm not even going to execute this okay then if we put an else Block in here then we can say hmm and let's say hmm that didn't work so well okay so if you put parenthesis between these and you can see that the logic becomes a little bit clean clear so and we can change this actually and we say let's say or okay and now all of a sudden you can see that this block of code is executed simply because either this should be true or this or both of them and since this part is not true it comes here it says your name is for indeed and your
1:04:00 age is 19. do you remember H was 19 and name was Foo then it executes this code simply because one of these branches is true either this or this or both and we know that that this is true so it executes this code so usually you don't write code actually like this thankfully in Swift usually your code should make more sense inside your if statements and you shouldn't have too many branches like this with ands and ORS and parentheses but sometimes if you have very complicated code if you're working with low-level Logic for instance At Your Service layer you might write code like this so you should be ready to kind of be able to handle and read this type type of code but I completely understand it it is a
1:04:46 little bit complicated to look at it if you're just looking at it like this and usually code like this is prefix with some common explaining why we're doing things the way we're doing them okay so that was it for our if and else playground in the next section we're gonna talk about functions so we can create a separate playground basically for functions after working with if and else statements we can go ahead now and work with functions we've seen a few examples of functions in Swift before and hopefully you already know what functions are so we don't have to go too much into details with functions but let's go ahead and explore some possibilities that we have with functions in Swift so command n in xcode or if you're in Visual Studio code I
1:05:31 think also command n and or control n on Linux and we are going to just search for playground in here playground and let's say blank playground in here and let's call it functions and then let's create it inside the main group and inside the main folder okay and I think would be perhaps a little bit better if we change the size in here so I'm going to go to themes in Midnight which I'm using I'm going to select all of these and then let's just change the font size perhaps to let's just say 36 perhaps or even yeah 30 36 I think is good enough so we see the code a little bit better okay so with functions in Swift you have the same possibilities as you have in many other programming languages such as
1:06:16 Python and dart and typescript JavaScript pretty much all the modern programming languages have some kind of same syntax and functionality for functions so let's have a look at a few examples so let's say we import Foundation I'm gonna get rid of that part to the left or Explorer so we can create functions in Swift with the func syntax or a keyword and then the name of the function and it should be camel case meaning that the first word of the function name should all be lowercase on all the subsequent words inside the function name should have or their first letter as uppercase and all the other letters as lowercase so here is an example of a function that takes no arguments and it has no return value so
1:07:02 let's say no arguments and no return value you can see all the if the first word has all its letters as lowercase and all the other words have their first letters as uppercase that follow the first word okay then we put uh parenthesis in here and we can put our arguments in between the parenthesis but this function doesn't have any arguments then we do curly brackets and then inside the curly brackets is the body of your function and here we're just going to say I don't have I don't know what I'm doing okay and the way that we write these things in playground is a little bit special because in Swift playgrounds whatever you type as the string will actually be printed inside the playground's preview
1:07:48 however this is not how you would actually write Swift Code so you would probably just issue a print statement okay but in playgrounds this does make sense so let's just do automatically run in here so our code gets executed automatically without us having to do anything and you can see nothing is being printed here simply because we're not calling or invoking our function now the process of invoking a function is just to tell to tell Swift that you want the code inside this function to be executed and the way to do that is just you use the function name no arguments and then with the parenthesis and any arguments that the function expects within the parenthesis now thankfully this function doesn't have any arguments so we don't have to pass any arguments within the parenthesis either and you
1:08:33 can see the value is then printed to our playground console in here okay now we can also have functions that have arguments so let's create a function that takes any integer value and adds the value of 2 to it but this particular function is not going to have any return value again so we're just going to have a look at how to pass arguments to a function so let's say func plus two so like this and then we take a value and you can see this is how you specify an argument to a function in Swift you just type name of the argument and then you type the data type and also there's another way of doing this and I'll explain it later and you can have two labels for a variable one an external label and an internal label which is
1:09:19 something quite unique to Swift you can have it in some other languages but not in all so here's an example of a function that internally creates a new variable let's call it new value and then takes that value and adds a 2 to it with a unary
…