How to use Optionals in Swift | Swift Basics #6

  Переглядів 12,406

Swiftful Thinking

Swiftful Thinking

День тому

Master the concept of optionals in Swift with this comprehensive tutorial! Learn how to handle optional values and avoid runtime errors by unwrapping optionals safely. Understand the power of optional binding and nil coalescing to gracefully handle optional values in your code. Gain the skills to work with optional chaining and handle optional types effectively. Start your journey to becoming a skilled Swift developer today and harness the flexibility and safety that optionals bring to your code!
00:00 Optionals
00:00 Intro
02:23 Welcome back!
03:33 Optionals
06:26 Unwrapping using Nil Coalescing Operator
09:57 Any Type can be Optional!
11:38 Examples
14:10 Unwrapping using If-Let Statement
19:24 Unwrapping using Guard Statement
23:40 iOS Interview Question
24:33 Complex If-Let
32:12 Complex Guard
33:48 Guard vs If-Let
34:37 Nested If-Let
39:45 Nested Guard
43:18 Optional Chaining
49:38 Review
50:16 Explicitly Unwrapping Optionals
53:02 Wrapping up
Website: www.swiftful-thinking.com
Discord: / discord
Source code: github.com/SwiftfulThinking/

КОМЕНТАРІ: 52
@denilsonwashuma1987
@denilsonwashuma1987 Місяць тому
best swift course on youtube currently
@thunderstorm1430
@thunderstorm1430 29 днів тому
agreed
@chandlerketelsen8702
@chandlerketelsen8702 7 днів тому
The videos get longer throughout the playlist! I really appreciate the content, thanks!
@shaxzodziyadullayev9115
@shaxzodziyadullayev9115 4 місяці тому
I liked this video. Deeper down into unwrapping optionals compared to other videos out there. Thanks.
@lolrie
@lolrie 9 місяців тому
Optionals confused the hell out of me until this video. The examples showing distinctions between guard let of all three vars vs. layered guard was especially helpful. Thanks so much!
@olatunji2542
@olatunji2542 23 дні тому
I understand optionals better now. Thanks a bunch
@Watashi10
@Watashi10 9 місяців тому
Very very good optionals tutorial. I loved all the examples! Thank you!
@wardog29
@wardog29 6 місяців тому
Nick, thanks so much for this one! Unwrapping and the use of guard was a gray area for me. You make it so easy to understand! Keep up the great work my friend!
@JuanFmTech
@JuanFmTech 8 місяців тому
it gets better and better
@jayvalance
@jayvalance 9 місяців тому
Great Playlist, The best way to learn Swift! i love your video. Thank you!!
@jameskayten6563
@jameskayten6563 8 місяців тому
Thanks very much for these well expressed lessons! It took me three days to get through the optionals as I could only deal with so much info inside of a couple hours. I'll likely forget half of what I learned, but at least there'll be bits of info that will service as hints going forth. There's a bit of confusion introduced in the last bit on chaining optionals. It's generally better to create copies of code blocks you're going to alter to illustrate concepts than returning to previous blocks and doing so. Each block the user creates (with commentary) is better left unedited avoiding typos and missed aspects like for instance the ?. I don't how others learn, but I need to follow along and type code to reinforce the info being conveyed. I'm also a lousy typist so I often get frustrated and lost.... But anyway - again - thanks very much Nick!
@vk1653
@vk1653 Місяць тому
Man this video is amazing, you broke this down very well and simple. I been trying to understand optionals for a while, esp the guard statement, but with more practice, I think I understand the concept. Thank you
@jankuna9479
@jankuna9479 3 місяці тому
Wow... great work. Thank you.
@30guarino
@30guarino 10 місяців тому
Wow...nice job on the explanation on optionals!
@SwiftfulThinking
@SwiftfulThinking 10 місяців тому
I know you’ve already completed the other playlists and know this already 😂 thanks though!
@DeclanBav101
@DeclanBav101 7 місяців тому
these are best swift courses , no words to express my gratitudes
@SwiftfulThinking
@SwiftfulThinking 7 місяців тому
Thank you!! These comments help 🙏
@JuanFmTech
@JuanFmTech 8 місяців тому
not and easy topic but i trust it will be important in the future, thanks for making such a detail explanation, I did some test printing things before and after unwrap and also the explicit of a nil to see it crash, it helped me understand a bit more.
@kwkistler
@kwkistler 9 місяців тому
Thank you, Nick, excellent tutorial I look forward to learning more about writing cleaner Swift code Keep up the great tutorials!
@aleksandertesenkov1421
@aleksandertesenkov1421 4 місяці тому
Excellent explanation.
@laundromatsuccess2145
@laundromatsuccess2145 10 місяців тому
Thank you Nik. Great info!!!
@SwiftfulThinking
@SwiftfulThinking 10 місяців тому
Thanks!
@awakeFromNib
@awakeFromNib 10 місяців тому
Excellent tutorial!
@SwiftfulThinking
@SwiftfulThinking 10 місяців тому
Thanks!
@toddlask
@toddlask 10 місяців тому
awesome channel!
@warrenscorner
@warrenscorner 10 місяців тому
Awesome! I have an idea for a calculation type app. The user would need to enter some values (how do convert a string to a double?) but of course when the app first opens there aren’t any values. Do these variables need to be optionals or can they be nil?
@bhavin_thummar
@bhavin_thummar 10 місяців тому
Thanks, Nick, great explanation... I have seen in many articles, there is one more approach to unwrapped. let optional: String? = "I love Swift" if case let unwrapped? = optional { print(unwrapped) } is it the correct way?(for such a case)
@user-to7yg5wm6d
@user-to7yg5wm6d 9 місяців тому
Awesome Man Awesome!!!!!!!🤓
@theflutterguy6301
@theflutterguy6301 2 дні тому
good stuff
@user-nc8nw4or6z
@user-nc8nw4or6z 10 місяців тому
You are the best dev content creator. Thanks! Hey, is your SwiftUI Bootcamp (Beginner Level) playlist outdated? I want to learn iOS 17 SwiftUI from start with your videos.
@SwiftfulThinking
@SwiftfulThinking 10 місяців тому
Hey! No the SwiftUI Bootcamp is all very relevant still. There are a few things that have been deprecated, but I’ve appended videos at the end of the playlist to address that. I will be adding iOS 17 videos once iOS 17 is released to the public.
@KevinCodeingology
@KevinCodeingology 2 місяці тому
​@@SwiftfulThinking STILL relevant?🙏
@SwiftfulThinking
@SwiftfulThinking 2 місяці тому
@@KevinCodeingology Very 😉
@panosjapan7
@panosjapan7 8 місяців тому
I always get error "Variable binding in a condition requires an initializer" if I refactor the `if let isNew = userIsNew` to `if let isNew` . ChatGTP says that I need to keep the first version so how come it doesn't work? I ran `xcrun swift -version` on my terminal and it says `swift-driver version: 1.45.2 Apple Swift version 5.6.1`. Is the version too old to work with this new syntax you use?
@jeff5881
@jeff5881 7 місяців тому
'nil requires a contextual type
@1zlach
@1zlach Місяць тому
Is there any way to help us visualize (via the console) functions "checkIfUserIsPremium" 1-6? It's quite confusing keeping the different constants and function names separated and understanding how each function works without seeing some final product.
@honey.badger
@honey.badger 8 місяців тому
i've watched this vid twice and still don't get it. gonna read the dev book further and come back to this vid later.
@filipjakub6303
@filipjakub6303 5 місяців тому
How did you comment more lines at once ?
@HaKaNOGUZ
@HaKaNOGUZ 5 місяців тому
Hi Nick. Did someone else record this part? When you started the coding section and suddenly your voice was changed. This was not your voice.
@AnimeWorld-pf9qy
@AnimeWorld-pf9qy 9 місяців тому
8:55 need to explain what ".description" do => gets you the value of "non-String variable" into a String value , so you can actually do "coalescing operator" and print a "string" value inside \( ) out of "Bool => non String dataType".
@saikrishnapatha4386
@saikrishnapatha4386 3 місяці тому
I want source code of all your examples, How can i download it or pls share path
@SwiftfulThinking
@SwiftfulThinking 3 місяці тому
GitHub @SwiftfulThinking
@CMessineo
@CMessineo 2 місяці тому
I'm able to follow the code. But I don't fundamentally understand why I would ever use an optional? What is the benefit of it over a non-optional? Why do I want to have variables that are null? Wouldn't I just rather have a boolean with a default of false, an integer with a default of 0, or a string with a fault of empty?
@SwiftfulThinking
@SwiftfulThinking 2 місяці тому
There are times where we can use default values to avoid optionals, but providing a default value is not the same as not having a value. Imagine fetching the user’s age that fails. You could have a default age of 0 or leave it as optional. If it’s optional, you can unwrap it to check that there is an age. If it’s defaulted to 0, it would appear no different than successful. You’d likely need extra logic to confirm it’s not the defaulted value. But high level optionals are fundamentally build into Swift. As you start to build apps you’ll realize it’s a net positive for you as the developer. It’s not a question of “why would I want this” but “how can I handle this” 😅
@CMessineo
@CMessineo 2 місяці тому
@@SwiftfulThinking Thanks so much for the further explanation. That "age check" is a nice example. Overall, I'm really enjoying your videos. I have a simple appthat I intend to build when I finish the lessons and I'm sure some of these things (like optionals) that don't seem that important now, will make more sense when I start coding the app.
@indomitabletr1834
@indomitabletr1834 10 місяців тому
Hi Nick 🤩
@SwiftfulThinking
@SwiftfulThinking 10 місяців тому
Helllllooooooooo
@darrenl8584
@darrenl8584 10 місяців тому
"promosm" 😭
@mosh_wash
@mosh_wash 10 місяців тому
Tremendous work💪 and very easy to follow🛵 Thanks a lot!
@SwiftfulThinking
@SwiftfulThinking 10 місяців тому
Thanks!
How to use Tuples in Swift | Swift Basics #7
13:44
Swiftful Thinking
Переглядів 7 тис.
OOPS Concept ROADMAP  | Complete OOPS for Placement Interviews
9:00
Love Babbar
Переглядів 373 тис.
Functions in Python are easy 📞
10:38
Bro Code
Переглядів 354 тис.
How to use Functions in Swift | Swift Basics #5
36:25
Swiftful Thinking
Переглядів 13 тис.
Swift Optionals - How to Unwrap (real examples)
14:20
Sean Allen
Переглядів 10 тис.
How to Code Pool in JavaScript
11:30
Fast Coding Tutorials
Переглядів 1,9 тис.
I learned to code from scratch in 1 year. Here's how.
41:55
Thomas Frank
Переглядів 300 тис.
do you know how "return" works under the hood? (are you SURE?)
5:08
Low Level Learning
Переглядів 342 тис.
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Переглядів 2,5 млн