1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Advanced Techniques in Swift

Guard Let

Contents

keyboard_tab
Advanced Swift
2
Struct Overview
PREVIEW6m 10s
6
Tuples
7m 23s
7
Guard Let
5m 52s

The course is part of this learning path

Start course
Overview
Difficulty
Advanced
Duration
51m
Students
3
Description

This course focuses on advanced techniques in Swift. You'll be introduced to structs and how we can use them in our code.

Transcript

Hi, within this lecture we're going to see a new concept called Guard Let. So far, we have been working with If Let to manage the option of the inside of Swift. But we're going to see Guard Let this time and it's very similar to If Let, but there are some differences in syntax and there are some differences in implementation as well. So, what are the differences and why do we use Guard Let or why do we use If Let in some certain cases? So, this is not an official way to teach this or this is not an official way to tell about Guard Let, but I'd like to remember it like this. So, if I'm certain that this is going to work and there is no other way to make this work, and if I am confident that I should guard this, I should have this value, then I'll use Guard Let, and it's kind of a negative thing. So, it asks me what will happen if that doesn't work first, and in If Let, it's kind of positive. We're not very certain that if this will work or not, but in Guard Let, we are certain that this will work. So, let me create a string and you will know what I mean. So, let's suppose that myString is 5, okay? But this isn't a string and I'm going to convert this to be an integer. And apparently, I have used myString before, up here we have used myString, so we have to change this to something else. As you can see you're redeclaring it. So, let's call this myNumber, but this is a string, not an integer. And we're going to convert this to be an integer using a function. So, I'm going to do this with Guard Let and  if let, and you will see the differences. So, first of all I'm going to convert this with  guard let and it will ask me for a string input and give me an integer as an output. And we're going to use  guard let in here. And of course we don't know how to use Guard Let yet, but we know how to use If Let, right? So, let me write another function called convertToIntegerIf, and this will again ask me for a string input, okay? And this will be a string and this will again give me an integer. And we can just say if let myInteger is int() myNumber or string input. Sorry, string input, because we're going to take this myNumber as a string input. And if that's the case, if we can make this, then I'm going to return my integer, right? And else if that doesn't work, I'm going to return zero. So, that's how we do it with if let. So, let's try to write the same thing with guard let. So, if I say guard let over here, I can again say my integer is int() string input. But this time not string interpolation, yep, this is string input. But this time I cannot even do that because that's not the proper way to use guard let, you have to say else before you open the curly braces, and this is asking for you what will happen if this doesn't work. In this  if let case it asks me what will happen if it works. So, in guard let, it's kind of negative as you can see. It's certain that this is going to work, and it's asking you for the first thing to do. If this doesn't work, what will happen? So, you can come over here and say return zero, right? And else you can just return myInteger over here, you can just say return myInteger. And as you can see under this function, we are certain that this is going to work and we are providing a value if this doesn't work, so we are really guarding it, okay? So, this is negative and confident, and if let is not that confident and it's kind of positive attitude. So, again, this positive and negative thing actually is for you to remember, this is not an official documentation or official way to teach you, but I believe this will make you a good way to remember this by so that you will know when to use which. Well, let's try both of them, right? So, let's print out, convertToIntegerIf, and again for string input, we're going to give my number, and in here we're going to call convertToIntegerGuard, and this time we're going to give my number again. So, let me try to run this and we will get the same result, right? Like 5, 5 because both functions are doing the same thing and instead of five, if I write Apple, the both functions will return me to zero. So,  guard let and if let eventually brings me the same result, but there are some differences when we implement them. So, I hope you enjoyed the advanced Swift section so far. We're going to stop here and continue within the next section.

 

About the Author
Students
420
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics