How to use Optionals in Swift | Swift Basics #6

Поделиться
HTML-код
  • Опубликовано: 25 дек 2024

Комментарии •

  • @denilsonwashuma1987
    @denilsonwashuma1987 9 месяцев назад +18

    best swift course on youtube currently

  • @matthew_cramer
    @matthew_cramer 8 дней назад +1

    I am convinced Nick is the nicest person on earth, and/or struck it big with some dream job as a mobile dev, because MY GOD. This content is amazing. I am hoping there is more content from you after this basics course :) If not, I will be reading the entire Swift Textbook next.

    • @SwiftfulThinking
      @SwiftfulThinking  8 дней назад +1

      You’re on course 1 of 13 🤙
      Watch order is here: www.swiftful-thinking.com/store

  • @leyfqq
    @leyfqq 3 месяца назад +2

    the best explanation of swift ever

  • @lolrie
    @lolrie Год назад +5

    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!

  • @jameskayten6563
    @jameskayten6563 Год назад +1

    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!

  • @shaxzodziyadullayev9115
    @shaxzodziyadullayev9115 Год назад +4

    I liked this video. Deeper down into unwrapping optionals compared to other videos out there. Thanks.

  • @Md.FahimAhmad
    @Md.FahimAhmad 3 месяца назад

    love it man. You are such a gem. Thanks nick.

  • @JuanFmTech
    @JuanFmTech Год назад

    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.

  • @chandlerketelsen8702
    @chandlerketelsen8702 7 месяцев назад +1

    The videos get longer throughout the playlist! I really appreciate the content, thanks!

    • @matthew_cramer
      @matthew_cramer 8 дней назад

      So do the lines of code for me... It went from 20 --> 200 in a few videos :)

  • @kwkistler
    @kwkistler Год назад +1

    Thank you, Nick, excellent tutorial I look forward to learning more about writing cleaner Swift code Keep up the great tutorials!

  • @sele_br129
    @sele_br129 7 месяцев назад

    A lot of information together but omg so useful! Thank you so much! Loved the video!

  • @30guarino
    @30guarino Год назад +3

    Wow...nice job on the explanation on optionals!

    • @SwiftfulThinking
      @SwiftfulThinking  Год назад

      I know you’ve already completed the other playlists and know this already 😂 thanks though!

  • @mosh_wash
    @mosh_wash Год назад +1

    Tremendous work💪 and very easy to follow🛵
    Thanks a lot!

  • @Declan-Jo6n
    @Declan-Jo6n Год назад

    these are best swift courses , no words to express my gratitudes

  • @vk1653
    @vk1653 9 месяцев назад

    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

  • @olatunji2542
    @olatunji2542 8 месяцев назад

    I understand optionals better now. Thanks a bunch

  • @wardog29
    @wardog29 Год назад

    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!

  • @toddlask
    @toddlask Год назад

    awesome channel!

  • @cxhello
    @cxhello 26 дней назад

    Why doesn't my Xcode have a warning prompt? How can I turn it on? My Xcode version is 15.2

  • @AnimeWorld-pf9qy
    @AnimeWorld-pf9qy Год назад

    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".

  • @MuhammadHuzaifa-t2s
    @MuhammadHuzaifa-t2s Год назад

    Awesome Man Awesome!!!!!!!🤓

  • @jayvalance
    @jayvalance Год назад

    Great Playlist, The best way to learn Swift! i love your video. Thank you!!

  • @gibber1sh-c6w
    @gibber1sh-c6w Год назад

    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?

  • @1zlach
    @1zlach 9 месяцев назад

    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.

  • @bhavin_thummar
    @bhavin_thummar Год назад

    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)

  • @elvisbucatariu
    @elvisbucatariu Год назад

    Very very good optionals tutorial. I loved all the examples! Thank you!

  • @stelluspereira
    @stelluspereira 5 месяцев назад

    thankyou Nick,
    For Non SwitData, objects above work well,
    but it seems if the object is from SwitData, no way i can delete optional input, for example :
    let input_ClientMst:ClientMst? // received from another view & i am checking for not nil
    ...
    if(input_ClientMst != nil)
    {
    modelContext.delete(input_ClientMst)
    }
    error Value of optional type 'ClientMst?' must be unwrapped to a value of type 'ClientMst'
    hope you can guide me , how to unwarp

  • @warrenscorner
    @warrenscorner Год назад

    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?

  • @m.d.2029
    @m.d.2029 7 месяцев назад

    Большое спасибо! Очень крутое объяснение!

  • @lowlydeveloper3693
    @lowlydeveloper3693 2 месяца назад

    "Very Nice!"

  • @JuanFmTech
    @JuanFmTech Год назад

    it gets better and better

  • @ArpitTripathi397
    @ArpitTripathi397 Месяц назад

    Its providing me error when I am writing the optional code. Error - main actor-isolated var 'userIsNew' can not be referenced from a nonisolated context
    guard let userIsNew, let userDidOnboarding, let userFavouriteMovie else{
    Is it because swift of the swift version because. it was working in the video
    Edit : got the answer , it is due to the swift version 6.0 . Swift came with the upgrade of complete concurrency checking is enabled by default. you have to add Main Actor annotation to make it work

    • @SwiftfulThinking
      @SwiftfulThinking  Месяц назад

      Just put @MainActor before your function declarations and it should go away

    • @ArpitTripathi397
      @ArpitTripathi397 Месяц назад +1

      @SwiftfulThinking could you please elaborate why we have to add Main Actor before my function declaration, i know its an update but can you please explain what it really does

  • @HaKaNOGUZ
    @HaKaNOGUZ Год назад

    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.

  • @laundromatsuccess2145
    @laundromatsuccess2145 Год назад

    Thank you Nik. Great info!!!

  • @awakeFromNib
    @awakeFromNib Год назад

    Excellent tutorial!

  • @CMessineo
    @CMessineo 10 месяцев назад

    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  10 месяцев назад +1

      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 10 месяцев назад

      @@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.

  • @wanwan-q2s
    @wanwan-q2s 4 месяца назад

    how to show declaration when we click on the variable?

  • @jonathanverbeet9234
    @jonathanverbeet9234 3 месяца назад +1

    I've got a question for when I try to access variables in a function. E.g. in this code:
    var userIsNew: Bool? = true
    func checkIfUserIsSetup() {
    if let isNew = userIsNew {
    }
    }
    The Compiler always tells me to add @MainActor before the "func". So when I press on fix the code I end up with this code:
    var userIsNew: Bool? = true
    @MainActor func checkIfUserIsSetup() {
    if let isNew = userIsNew {
    }
    }
    What is the problem and how do I fix that? Can anyone tell me?
    Sorry for my English. Im not a mother tongue.

    • @Toni-h1p
      @Toni-h1p Месяц назад

      I have the same problem... please help! I'm stuck and can't seem to move on

    • @ArpitTripathi397
      @ArpitTripathi397 Месяц назад

      Same here , i dont know if its my mistake or we have to mention main actor in the code

  • @jankuna9479
    @jankuna9479 11 месяцев назад

    Wow... great work. Thank you.

  • @saikrishnapatha4386
    @saikrishnapatha4386 11 месяцев назад

    I want source code of all your examples, How can i download it or pls share path

  • @Michelsvlogs1
    @Michelsvlogs1 2 месяца назад

    Absolutely love the series, I have been getting bug messages from xcode, just by copying everything that he has written,"Main actor-isolated var 'userIsPremium' can not be referenced from a nonisolated context" if anybody can help explain I would really appreciate it

    • @SwiftfulThinking
      @SwiftfulThinking  2 месяца назад +1

      If you’re building this in Xcode, the new versions of Xcode run Swift 6, which is a more strict compiler. You can turn this off by going to the project navigator, clicking “Build Settings”, search for Swift Language Version, and revert it down to Swift 5.
      The warning is telling you that pieces of code can only be accessed on the “MainActor” which is the default thread where most of your code runs anyway. With the new Swift 6 compiler, we are now forced to explicitly tell the compiler if we’re are on the MainActor. This can usually be solved by just putting “@MainActor” before the “func”, “var”, “let”, “class” or other keywords.
      The MainActor is a more advanced topic that I cover in the Swift Concurrency playlist, but not something I would worry much about just yet!

    • @fabulistfm6741
      @fabulistfm6741 Месяц назад

      I am experiencing the same error, so you're not alone. I have not yet found a solution.

    • @CDsCartoons
      @CDsCartoons Месяц назад

      @@SwiftfulThinking I'm not able to switch to Swift 5 in the playground. How do I find the settings for this?

    • @SwiftfulThinking
      @SwiftfulThinking  Месяц назад +1

      @@CDsCartoonsim not sure you can change the compiler in Playgrounds. If you but “@MainActor” before the function it should work

  • @theflutterguy6301
    @theflutterguy6301 7 месяцев назад

    good stuff

  • @filipjakub6303
    @filipjakub6303 Год назад

    How did you comment more lines at once ?

    • @SabinDahal2061
      @SabinDahal2061 3 месяца назад

      you can use
      /* line1
      line2
      line3 */
      else use command+/ (selecting multiple lines to comment)

  • @aleksandertesenkov1421
    @aleksandertesenkov1421 11 месяцев назад

    Excellent explanation.

  • @honey.badger
    @honey.badger Год назад +3

    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.

  • @indomitabletr1834
    @indomitabletr1834 Год назад

    Hi Nick 🤩

  • @jeff5881
    @jeff5881 Год назад

    'nil requires a contextual type

  • @darrenl8584
    @darrenl8584 Год назад

    "promosm" 😭