|
hey everyone, I'm trying to go through the lessons in day 14 again so that I can solve checkpoint 9. I apologize in advance if I am not learning as fast but I can promise that I am really trying. Here's one thing that I wasn't able to notice before but I only noticed right now while rewatching the "How to unwrap optionals with guard" lecture. in guard let, why is it that we immediately go to the else block? why can't guard let have an if block? This is confusing me because I thought if else statements should always follow the following format
However, guard let doesn't follow this format. Why? And I can't see the purpose of guard let when if let also covers the same thing. This is making me have a hard time grasping the concept and use case of guard let. I understand that guard let only runs if our optional DOES NOT have a value inside. Can't if let also cover it since it'll also run the block after the if statement if our optional also doesn't have a value? It'll only run the if let statement if there's a value inside making it better than guard let. Doesn't that make the concept of guard let kind of redundant? |
|
You shall not pass!When I first read about Swift's If you knew the password, or had the right credentials, the Beefeaters let you pass. You were free to enter and go where you pleased. If not, you weren't even allowed inside. End of story. In a
If the guard sees you're trying to get in without a ticket, he denies your visa. No entry for you! |
|
Early exit from a function improves readability and is probably the main attraction of But A lot of times, you can use either a
You can see that in |
|
I'm a bit confused here
I thought guard let will only run if Swift unwraps our value and finds out that the unwrapped value DOES NOT contain a value. If the unwrapped variable has a value, it won't run the guard let statement. Is my understanding wrong? In this line in the code,
Doesn't that mean it goes inside the else block and returns nothing or (goes out the function since it's a return statement) I also have another question. This is my first time encountering a not (!) operator after a variable
doesn't ! signify not and is supposed to be for booleans? Lastly, is my understanding in this part correct?
Since we unwrapped entryPass using guard let, and assigned a parameter ticket into it, that means we can use the passed parameter (entryPass) inside the whole attemptEntryIntoTowers() function? (although I'm not quite sure what selectIcon is for). Also, why did you mention that
I think I'm still a bit confused with the concept of guard let. I always assumed that it'll only run the else statement if the unwrapped optional is nil/empty. Apologies for the barrage of questions. I just wanted to make sure that I really understand this but right now, I'm really having a hard time grasping this concept. I do get the beefeaters example/analogy though! |
|
I see, so contents is available in the whole function after the guard let block while contents is only available inside the if block in the if let right? |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.