Learn Swift for Beginners - Ep 13 - Optionals

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

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

  • @CodeWithChris
    @CodeWithChris  6 лет назад +1

    QOTD ⚡Are optionals getting you down? Let me know! They were definitely hard for me when I first started!👇 Kick start your own app with my free 7 Day App Action Plan ➡️ bit.ly/7dplan

  • @immrpandabear
    @immrpandabear 6 лет назад +2

    You're one of the few people who actually explain everything they're doing. Thank you.

    • @CodeWithChris
      @CodeWithChris  6 лет назад +1

      Thank you! Really trying to guide the best we can!
      -Adrien

  • @nogripz
    @nogripz 7 лет назад +14

    Your gift box analogy is great! I'll think of optionals this way from now on. Keep up the good work Chris!

  • @longdongsilver4719
    @longdongsilver4719 6 лет назад +1

    You are the one who made it clear for me how optionals work and what they are for. Thank you for that! Great work.

  • @ninumedia
    @ninumedia 3 года назад

    You explained optionals in a way that's clear and easy to understand! Thank you for using the analogy about a box and unwrapping the present.

    • @CodeWithChris
      @CodeWithChris  3 года назад

      So thankful for your kind words, Nirdhar! We'll keep on working hard to produce more high-quality lessons just for you guys! -Arthur

  • @juliamiasoiedova5001
    @juliamiasoiedova5001 7 лет назад +5

    Wow! Your explanation about optionals is the best - short and easy-to-understand! Thank you very much, Chris!

  • @ufookoro1
    @ufookoro1 5 лет назад +1

    Phew!!! Thank you for the Optionals lesson.
    I have had a hard time when building apps small apps.
    I have in all the case chosen to force unwrap and not bother to perform the checks, bad move on my part.
    Had to go back to basics and learn this topic from scratch!!!
    Now I see the importance, and thank you very much

    • @CodeWithChris
      @CodeWithChris  5 лет назад

      Swift is everything about Optionals! Watch out!
      @AdrienVillez

  • @etaosin
    @etaosin 6 лет назад +1

    I wonder how anyone could give unlike for this video. It is so well explained that it looks like a good adventure movie

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      :) First time i heard that analogy, but i really like it!

  • @samuelfarmer171
    @samuelfarmer171 7 лет назад +1

    Another great video. Just could not grasp what optionals were or why someone would use them before watching this. Now I do. Hope you continue to post great videos like this!

    • @CodeWithChris
      @CodeWithChris  7 лет назад

      I will as long as you keep watching :)

  • @riccygee
    @riccygee 7 лет назад +2

    Great video as usual Chris. For the newbies, you could also use guard statements as an alternative to avoid the nasty if/let nests of doom. Guards can be extremely useful when you need your object to have a value. For example..
    let post = BlogPost()
    post.title = "Hey your title is here!"
    func foo() {
    guard let actualTitle = post.title else {
    print("No title here buddy")
    return
    }
    print(actualTitle)
    }
    foo()
    **console prints Hey your title is here!**

    • @CodeWithChris
      @CodeWithChris  7 лет назад +1

      Thanks for the feedback and showing others Riccy! I'm still getting used to incorporating guard into my own coding habits. Here's a good article for anyone who's interested in learning more about when to use guard vs if: www.natashatherobot.com/swift-when-to-use-guard-vs-if/

    • @riccygee
      @riccygee 7 лет назад

      Of course, keep up the great work!

  • @mohammedseif28
    @mohammedseif28 7 лет назад

    thank you man for your effort, you style is very simple to understand, I hope you keep up the good work and complete this series, help us beginners learn how to code
    thanks a lot man, may Allah reward you for that good deed :)

  • @detonate2537
    @detonate2537 7 лет назад

    Great video series! really helpful for someone who doesn't have much time to dedicate in learning how to code

  • @RajrkSunny
    @RajrkSunny 7 лет назад +1

    Started learning swift 2 days back, I had no clue what optionals meant and searched every where but couldn't get a neat explanation as yours. Every video of yours is clear and nice..... please do some videos about tableviews and Api's.:-)

    • @CodeWithChris
      @CodeWithChris  7 лет назад

      Thanks Raj! Your suggestion is recorded and I do intend to cover those! Thanks!

  • @prasadsuryawanshi3185
    @prasadsuryawanshi3185 7 лет назад

    Thank Chris. I like the way you teach. It cleared all my doubts..

  • @theprettyunicorn5612
    @theprettyunicorn5612 7 лет назад +1

    Thanks! Clear and easy to understand as usual!

  • @brucekim5166
    @brucekim5166 5 лет назад +1

    I been watching every video you made for Swift

  • @carlosfernandez6470
    @carlosfernandez6470 7 лет назад

    Awesome. Very clear explained. Thanks Chris

  • @kapilsoni3106
    @kapilsoni3106 7 лет назад +1

    Wow Burden of this optional is clear now. Now i can force unwrapp my idea to creat app. hahah! Thanks Chris

    • @CodeWithChris
      @CodeWithChris  7 лет назад

      Hey Kapil, thanks for watching as always!

  • @andresgonzalez1811
    @andresgonzalez1811 6 лет назад

    Thanks for putting out content like this! I was hoping you would explain how to use the var author:Person? within optional binding...but that's alright, challenge accepted, I'll try to figure it out.

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      We can always make one full video about this! We are really trying to keep the videos as short as possible while still covering the essential. (because a 20 to 30 minutes, people usually start to lose focus).
      Stay tuned for more on RUclips and if you cannot wait, we have more covered on www.codewithchris.com
      -Adrien

  • @pineapplepizza4016
    @pineapplepizza4016 7 лет назад

    Good explanation on what optionals are and how to unwrap them, although I'm still uncertain of what instances I would actually use them.

  • @cavinalbertbelga5090
    @cavinalbertbelga5090 7 лет назад +2

    Thank you for your tutorials! it helps a lot :)

  • @brucekim5166
    @brucekim5166 5 лет назад +1

    I need to be able make a good apps by 2020 March 10th and it was March 10 2019 when I started thanks to that I got a MacBook Pro with touchbar though and I needed a person to teach me and when I found you I was amazed by how good you though you should work as a MIT professor for computer science

    • @CodeWithChris
      @CodeWithChris  5 лет назад

      Thank you for the kind comment! Glad we can help you get started in iOS! Good luck for your project! See you in 2020!
      @AdrienVillez

    • @kamilialatifah
      @kamilialatifah 5 лет назад

      Best of luck for you! Are you planning to create good apps for Apple Dev Academy entrance or WWDC?

  • @sanzharserik9835
    @sanzharserik9835 5 лет назад +1

    great video thanks!

  • @bdziamara6376
    @bdziamara6376 5 лет назад +1

    at 6:08 The box analogy made me realize that with optionals, it becomes Schroedinger's Variable!

  • @behindyoureyes911
    @behindyoureyes911 7 лет назад +1

    so good dude! man killin it bro

  • @BreatheDigital
    @BreatheDigital 7 лет назад

    Needed this....thanks!

  • @danuff
    @danuff 6 лет назад +1

    OMG THANK YOU for this video!

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      Sure! Looks like we made something click and now things make sense! 😉
      @AdrienVillez

  • @_kd
    @_kd 7 лет назад +1

    Great video Chris!

    • @CodeWithChris
      @CodeWithChris  7 лет назад

      Love your username and great first tutorial! Thanks for watching!

    • @_kd
      @_kd 7 лет назад

      CodeWithChris no problem, thanks again!

  • @DanGarGuitar
    @DanGarGuitar 7 лет назад +1

    Thank you, great video!

  • @alexwolfeboy
    @alexwolfeboy 7 лет назад

    Chris, you forgot one more way to deal with optionals, which stumbled upon while following along."Your a \(you.somevalue ?? "Default value")" assuming that you is a class and somevalue is set to either a string or nil

  • @huntert3195
    @huntert3195 7 лет назад

    Thanks, Chris

  • @stevenmylot
    @stevenmylot 7 лет назад +1

    I made a suggestion on some other channel I think to post a video on optionals and his greatness answered lol

  • @smartlight86
    @smartlight86 7 лет назад +1

    thank you Chris you're awesome

    • @CodeWithChris
      @CodeWithChris  7 лет назад +1

      You're awesome too for learning with me haha :)

  • @mrkattm
    @mrkattm 7 лет назад

    Question, is the data in a class public/global and changeable from outside the class object? You were able to set post.title = yo from outside object without sending a message to the object's member function (in this case there was none), this seems very dangerous. Great video series, thanks for sharing and keep up the great work

  • @motocyclediaries
    @motocyclediaries 4 года назад +1

    How to unwarp the property author that is of data type Person? I am first time coder so, apologies for asking stupid question.

    • @CodeWithChris
      @CodeWithChris  4 года назад

      Hello, if since the author inside the class blogpost is another class then just use another dot (.)
      for example blog1=blogpost()
      blog1.author.name to get the name of the author
      - Francis

  • @santiagdc5731
    @santiagdc5731 5 лет назад +1

    Thank you!!

  • @flandino
    @flandino 6 лет назад +2

    The only thing i found confusing was that to check if the variable was nil or not you didn't unwrap it first. You did need to force unwrap it with the exclamation point to use it as a string for the print statement. How can you check the assignment in the if-let statement before unwrapping it first, and how can you know if it is nil or not nil without unwrapping it first. I still don't understand why the box metaphor doesn't work in that case...

    • @badumtish3610
      @badumtish3610 6 лет назад +1

      My understanding is in the example where we test for nil
      if post.title != nil {
      print(post.title! + " salut!!")
      }
      we are forcing it to unwrap, meaning that even though we know that it can never execute *print(post.title! + " salut!!")*, since we checked if it was equal to nil first, the code will always operate fine. However, in
      if let actualTitle = post.title
      {
      print(actualTitle + " salut")
      }
      we actually are unwrapping it, just in a different way. When we write *if let actualTitle = post.title*, we are saying that we have no idea what is inside post.title. So, when we use *if let actualTitle = post.title*, we set actualTitle to what ever post.title is, and the if statement does not execute in the instance of actualTitle being set to nil. Thus, we have unwrapped it to see what was inside, which was either nothing, in which case we ignore the print command, or we get something, and then we execute the print command.
      Also, remember that nil is a fancy way of saying nothingness, and means nearly the exact same thing as the word null, which is used in other programing languages instead of nil.
      Again, this is my understanding of what is going on.
      Here is some example code with comments that might help.
      //Unwrap and test if it is not nil
      if let actualTitle = post.title{
      //if so, we have something useful.
      print("Something awesome is here!")
      }
      else { //otherwise, we have nothing, as the condition fails, and execute the following code.
      print("Ah, man. The box was empty.")
      }
      I hope this helps.

  • @topblogger5234
    @topblogger5234 7 лет назад

    a great video for beginner like me ^^

  • @mlouism1
    @mlouism1 6 лет назад +1

    Are you teacher? If not, you are like them! Fine pedagogy!

  • @ab0ut7dragons
    @ab0ut7dragons 6 лет назад

    so for //testing for nil at 14:47, why cant you just have one if statement and one else statement? why two if statements?

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      This is correct! You can use else for the other branch of the IF Statement.
      This video was mostly to show how to do it using == or !=.

  • @ShreyasRavibighero6
    @ShreyasRavibighero6 5 лет назад +4

    Lol i'm understanding optionals by thinking of Schroedinger's cat; you don't know whether it's dead inside or alive without opening it

    • @CodeWithChris
      @CodeWithChris  5 лет назад

      Wasn't aware about this, so I Googled!
      Here for the curious: en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat
      @AdrienVillez

  • @gabrielhayes3560
    @gabrielhayes3560 7 лет назад +1

    Thank you

  • @kennyfalcao7500
    @kennyfalcao7500 6 лет назад +1

    Hello @CodeWithChris we have a control on swift language can you help us please ?

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      If all the videos on our RUclips page aren't helping enough, drop us a message on our Community Group. We will help you right there. bit.ly/CWCCommunity
      -Adrien

  • @nirbarzilay6947
    @nirbarzilay6947 7 лет назад +2

    Hi Chris
    About the object "name" in the class Person you didn't explain how to use it.
    I have tried to use it without success until i realized it was necessary to make the object name as 'optional'.
    At next time please explain things like this more clearly

    • @CodeWithChris
      @CodeWithChris  7 лет назад +1

      Hey Nir, thanks for your feedback! I have an upcoming video on optionals that explains the concepts more clearly!

    • @nirbarzilay6947
      @nirbarzilay6947 7 лет назад +1

      i'm waiting to see it, you do a fantastic job here.
      thank you :)

  • @raghavanramaswamy8978
    @raghavanramaswamy8978 6 лет назад

    @13:11 when you print out post.title! why does the print statement say Optional("yo")

    • @30guarino
      @30guarino 6 лет назад

      Raghavan Ramaswamy because he wrote the code
      post.title=yo

    • @raghavanramaswamy8978
      @raghavanramaswamy8978 6 лет назад

      30guarino why is there the word optional there before it

    • @30guarino
      @30guarino 6 лет назад

      Raghavan Ramaswamy because if you look in the middle of the screen right above the text ( //Optional Binding )
      He gave “post.title” the meaning “yo”
      It’s not in any curly brackets either so it’s almost like a new code on it’s own

    • @30guarino
      @30guarino 6 лет назад

      Raghavan Ramaswamy make no mistake about I’m only 3 days in to this foreign syntax ....But repetition is the mother of all Skills

  • @awwabasad1117
    @awwabasad1117 3 года назад

    Hey, just curious…
    Have you done any other programming language before swift? If so, what?
    Just wonderin….🤗🤗🤔

    • @CodeWithChris
      @CodeWithChris  3 года назад +1

      I'll have to ask Chris, haha! :-) -Arthur

  • @Wuttasi
    @Wuttasi 7 лет назад +1

    Awesome :)

  • @tomlong7392
    @tomlong7392 6 лет назад

    hello , I want to share this in China,if you allow. can you gei me a zip about all zhe videos.your classes are so nice! LOVE

    • @tomlong7392
      @tomlong7392 6 лет назад

      give

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      I'm aware RUclips is not accessible in China. I will have to look for alternatives.

  • @DamLee88
    @DamLee88 7 лет назад +2

    gooooood
    very veryy godddddddd

  • @windu122
    @windu122 6 лет назад +1

    Hi. Thanks for videos. Why I getting? "error: Optionals.playground:5:9: error: expected pattern
    var = name ""
    ^"

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      If we could see the code, that would be helpful. Can you post a screenshot in our #CodeCrew group?
      bit.ly/CWCCommunity
      Thanks!
      @AdrienVillez

  • @12kvic
    @12kvic 7 лет назад

    when i tried downloading Xcode it said Xcode can’t be installed on “Macintosh HD” because OS X version 10.11.5 or later is required. i tried updating but says no update available, help please

  • @aryamannkhare9505
    @aryamannkhare9505 7 лет назад

    The cheat sheet are not working
    PLEASE HELP ME WITH THIS.
    I TRIED MANY TIMES.

  • @sanzharserik9835
    @sanzharserik9835 5 лет назад +1

    gift box ))

    • @CodeWithChris
      @CodeWithChris  5 лет назад

      Xmas every day with Swift!
      @AdrienVillez

  • @brucekim5166
    @brucekim5166 5 лет назад +1

    I gave a empty gift box to a friend and we broke up because I said the empty gift box was his birthday present

    • @CodeWithChris
      @CodeWithChris  5 лет назад

      Well, he got a box at least!
      @AdrienVillez

  • @abdulrahmanahmad7399
    @abdulrahmanahmad7399 6 лет назад +1

    ttttttttthhhhhhhhhhhhhhhhhink uuuuuuuuuuuuuuuuu

    • @CodeWithChris
      @CodeWithChris  6 лет назад

      Youuuuuuuuuuuuuuuuuuuuuuuuuur Wellllllcccoooooooooooooooooooommme!
      - Adrien

  • @kennyfalcao7500
    @kennyfalcao7500 6 лет назад +1

    We are in bts sn in aurillac please HELP UUUUUUS

  • @MrNiceone99
    @MrNiceone99 7 лет назад +1

    I think Swift is really stupid language. They are overcomplicating concepts just so that it differs from robust languages like C++/Java. In those languages you choose initialise or not a variable in a simple readable way. Swift is a overkill in my opinion. But the tutorials are amazing. Thanks bro :D

  • @D_a_r828
    @D_a_r828 5 лет назад +1

    Thank you very much!

    • @CodeWithChris
      @CodeWithChris  5 лет назад

      Thank you for learning with us!
      @AdrienVillez

  • @hassanduhair5180
    @hassanduhair5180 7 лет назад +1

    Awesome :)

    • @CodeWithChris
      @CodeWithChris  7 лет назад

      Hey Hassan, thanks for watching and commenting!