How to use Optionals in Swift | Swift Basics #6

Поділитися
Вставка
  • Опубліковано 20 гру 2024

КОМЕНТАРІ • 71

  • @denilsonwashuma1987
    @denilsonwashuma1987 9 місяців тому +16

    best swift course on youtube currently

  • @matthew_cramer
    @matthew_cramer 3 дні тому

    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  3 дні тому +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

  • @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!

  • @lolrie
    @lolrie Рік тому +4

    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!

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

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

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

  • @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!

  • @chandlerketelsen8702
    @chandlerketelsen8702 7 місяців тому +1

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

    • @matthew_cramer
      @matthew_cramer 3 дні тому

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

  • @toddlask
    @toddlask Рік тому

    awesome channel!

  • @mosh_wash
    @mosh_wash Рік тому +1

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

  • @olatunji2542
    @olatunji2542 8 місяців тому

    I understand optionals better now. Thanks a bunch

  • @m.d.2029
    @m.d.2029 6 місяців тому

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

  • @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)

  • @Declan-Jo6n
    @Declan-Jo6n Рік тому

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

  • @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?

  • @cxhello
    @cxhello 21 день тому

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

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

  • @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!

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

  • @saikrishnapatha4386
    @saikrishnapatha4386 11 місяців тому

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

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

  • @wanwan-q2s
    @wanwan-q2s 4 місяці тому

    how to show declaration when we click on the variable?

  • @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)

  • @Michelsvlogs1
    @Michelsvlogs1 Місяць тому

    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  Місяць тому +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

  • @jayvalance
    @jayvalance Рік тому

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

  • @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 Місяць тому

      @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

  • @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?

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

  • @MuhammadHuzaifa-t2s
    @MuhammadHuzaifa-t2s Рік тому

    Awesome Man Awesome!!!!!!!🤓

  • @awakeFromNib
    @awakeFromNib Рік тому

    Excellent tutorial!

  • @laundromatsuccess2145
    @laundromatsuccess2145 Рік тому

    Thank you Nik. Great info!!!

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

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

  • @lowlydeveloper3693
    @lowlydeveloper3693 2 місяці тому

    "Very Nice!"

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

  • @theflutterguy6301
    @theflutterguy6301 7 місяців тому

    good stuff

  • @jankuna9479
    @jankuna9479 11 місяців тому

    Wow... great work. Thank you.

  • @aleksandertesenkov1421
    @aleksandertesenkov1421 11 місяців тому

    Excellent explanation.

  • @JuanFmTech
    @JuanFmTech Рік тому

    it gets better and better

  • @indomitabletr1834
    @indomitabletr1834 Рік тому

    Hi Nick 🤩

  • @jeff5881
    @jeff5881 Рік тому

    'nil requires a contextual type

  • @darrenl8584
    @darrenl8584 Рік тому

    "promosm" 😭