How to use the ternary conditional operator for quick tests – Swift for Complete Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] there's one last way to check conditions inside swift it's called the ternary conditional operator and when you see it you'll probably think why on earth would i ever want to have that in my code to be fair i used it very rarely previously as well however it has become very important inside swift ui so it's important you do learn it this is called the ternary conditional operator and understand where the name comes from i want to show you a very very small slice of code basic mathematics two plus five now that plus sign is an operator it works on pieces of data given to it and this one particular is called a binary operator as is star minus equals equals and more and they have that name because they all operate on two pieces of data that's all the binary means here it has two pieces of input coming in and it acts on that to get its result the ternary operator gets its name because it acts on three pieces of data at a time in fact it's only one ternary operator in swift the ternary conditional operator so often here it just called the ternary operator it's the only one we have anyway enough about names what does it actually do well this thing uh let us check a condition and then send back either one value or another value depending on the result of that condition for example we can make a constant called age so the 18 here and i'll make another constant called can vote set to either yes or no depending on the value of age and that's can be used for with a turning operator by saying let can vote equals age is greater than or equal to 18. question mark yes colon no and when this code runs canva will be set to yes because age is greater than or equal to 18. it's exactly 18. so as you might be able to see in the syntax jumble in front of you the turning operator has three parts first up the condition to check in our case we're saying is age greater than or equal to 18. then we have something to send back when that condition is true in our case we want to send back yes they are greater than or equal to 18. and finally something to send back if the condition is false and this makes it identical to an if else statement condition first if age growth equal to 18 then if that's true yes else no very very similar there's a condensed form of that if it helps to remember the order scott micho has a brilliant mnemonic here um and you can find him on twitter at scott me show his mnemonic is wtf which stands for what are we checking what to do if it's true and what to do if it's false in this case our condition the what are we checking is is age greater than or equal to 18. the t the true what to send back if our condition is true make can vote yes and the f what to do if k if a is not greater than 18 send back no the false statement here let's look at some other examples live in xcode starting with a nice easy one uh we're going to read an hour in 24 hour format and then print one of two messages so i'll say let hour equals 23 and then print our condition hour is less than 12 if that's true we'll say it's before noon if that's false we'll print its afternoon now in this case hour is greater than 12 so we can bring it back and it'll say it's afternoon which makes sense or we could do another one that reads what's in an array is an array empty or not and prints a message if it is empty otherwise the count of the array so we could say uh let names equals array of jane and kaylee and mal and let crew count equals if names is empty that's our condition then send back no one otherwise if named is not empty send back names dot count people and finally print crew count let's run that back boom three people now this gets a little bit harder to read when your condition involves equals equals you're checking for equality because you have equals and equals equals at the same time it's a bit hard to read at first let's see it here let's say there's an enum called theme with case light and dark i have two possible options i'll say we have a constant called theme equal to theme dot dark by default and now for our ternary let background equals our condition theme is equal to dark dot dark sorry if that's true send back black if that's false send back white and then print background and that code's valid it'll print out dark black sorry boom there we go and the equals and equals equals it's a bit hard to read at first but break it down this is our condition the what to check w this is our t what send back if it's true and it's our f what's in back if the conditions false if it helps you can put the condition in parentheses like this ah just make it clear that's the condition it's down to you now at this point you might be wondering what's the point how is this possibly useful we already have if and else if and else available to us why do we need to have this ternary conditional operator and i know it's not a great answer but you're going to have to trust me on this there are times particularly with swift ui when we have no choice we must use a ternary and you can sort of see what the problem is if we look at our print line up here to print this performing or afternoon if you want to write that out as an if you might have something like this um if hour is less than 12 like this then it's afternoon else like this sorry but before noon otherwise it's afternoon you might try and write that that's kind of what we mean here but you can see swift isn't happy swift does not allow us to have this kind of code inside a call to print and there's a subtle distinction here between what we call expressions and statements the simple fact is we can't have logic like if or switch or similar inside this print it's just not allowed you can't do it that way you might try something else which is valid but i wouldn't recommend it which is to move the print inside the condition so you'd say something like if r is less than 12 print it before noon else print it afternoon and then remove the inner brace and that second one works fine here you can uh you play you can do that that's allowed i made a typo is it complaining at me no it's fine that's allowed just a bit of time to catch up there a little bit um but it becomes almost impossible in surf dui where you've got to have condition condition condition condition a lot of these things stacked up sometimes and having lots and lots of ifs makes an absolute mess of your code so even though you might look at ternaries and say well why would i ever use that please trust me it does matter you
Info
Channel: Paul Hudson
Views: 4,994
Rating: undefined out of 5
Keywords:
Id: FToxl_63Unw
Channel Id: undefined
Length: 8min 43sec (523 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.