Introduction to Swift: Initializers (init method) part 2

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

КОМЕНТАРІ • 29

  • @shubhamjasani3176
    @shubhamjasani3176 4 роки тому +1

    Thank you for very good explanation 😊 keep it up

    • @CodeCat15
      @CodeCat15  4 роки тому

      Thank you for your comment, if you have any questions please feel free to ask and share this channel with your ios friends and team as well 😊

  • @bhanushekharsomala9737
    @bhanushekharsomala9737 Рік тому +2

    You are superb in explaining ununderstable things.
    You deserve this channel.
    I really like your way of explaining things.
    Please do many visa in swift projects.
    I am a swift developer.
    I am your die-hard follower.
    Thank you so much.

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

      Hello Bhanushekhar, welcome to the channel and thank you for your kind words. I hope you find the contents of this channel helpful and if you have any queries please feel free to ask them.

  • @abhijnanmaji2682
    @abhijnanmaji2682 2 роки тому

    Simple, Clear, Useful Explanation for Interview and work.

  • @pratimasuryavanshi8797
    @pratimasuryavanshi8797 2 роки тому +1

    simple and clear. Thank you for sharing, Keep doing the Awesome work

    • @CodeCat15
      @CodeCat15  2 роки тому

      I am glad the video was helpful, thank you Pratima and welcome to the channel

  • @abhayshukle3693
    @abhayshukle3693 2 роки тому

    It was very helpful, kindly explain about required init as well. Thank you.

  • @ganeshdeore5557
    @ganeshdeore5557 3 роки тому

    Really Good explanation.. Thanks alot...!!!

  • @vyankatesha.shivnikar696
    @vyankatesha.shivnikar696 8 місяців тому +1

    Hi Ravi , awesome video man, one question what shall we prefer giving default value in init or create convenience init and there give value

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

      It depends on the use case as to what you are trying to achieve and do by providing those default values.

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

    Hi Ravi, I have noticed 2 things, please correct me if i'm wrong
    1. if we are not using convenience key word, we wont be able to call self.init in init block.
    2. We cannot assign values to properties in convenience init and can only call designated init.

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

      you won't be able to do anything with self because to use the self keyword the object has to be present in memory, so in a convinience init if you try to initalize a variable you will get a compile time error stating that the self cannot be used before it's initalization something like that.

  • @vyankateshshivnikar7932
    @vyankateshshivnikar7932 2 роки тому +1

    Very helpful video...Just add required init if possible so no need to search for required init video separately...

    • @CodeCat15
      @CodeCat15  2 роки тому +1

      If my memory servers me right, i think I had required init somewhere if not then please let me know I'll make a video on it

    • @vyankateshshivnikar7932
      @vyankateshshivnikar7932 2 роки тому

      @@CodeCat15 ok sure👍🙌

  • @prachibile2798
    @prachibile2798 3 роки тому +1

    It was really helpful :)

    • @CodeCat15
      @CodeCat15  3 роки тому

      Awesome Prachi I am glad it was helpful

  • @neerajgupta8241
    @neerajgupta8241 4 роки тому +1

    Very good Explanation and can you make a video covering Deinitializers in Swift

    • @CodeCat15
      @CodeCat15  4 роки тому

      Hey Neeraj, checkout the video on ARC where i have covered deinit
      Link: ua-cam.com/video/XAlXnApplk0/v-deo.html

  • @vyankateshshivnikar7932
    @vyankateshshivnikar7932 2 роки тому

    Required init?

  • @ramgade2433
    @ramgade2433 4 роки тому +1

    One Question here, why we need a convenience initializer? If anyways we are calling to the primary initializer. And we can also overload the main init method for the same

    • @CodeCat15
      @CodeCat15  4 роки тому +2

      @Ram: let's start the discussion with what the documentation says
      "You do not have to provide convenience initializers if your class does not require them. Create convenience initializers whenever a shortcut to a common initialization pattern will save time or make initialization of the class clearer in intent."
      Link: docs.swift.org/swift-book/LanguageGuide/Initialization.html
      Convenience init should only be used if you need them as stated in the documentation.
      Convenience init makes more sense if your class has 10 properties and with user-defined init you need to add all those 10 properties as parameter for the init,
      whereas with convenience I can just expose one or two parameters and give all the other 8 parameters as default value inside the convenience init this helps me to keep my code clean to some extent than having a init with 8 default parameter values.
      When you talk about overload, yes that's another way of doing it but if you just give value to just 2 properties then what about the other 8? The swift compiler will give you an error asking what values you need to provide for all the other 8 stored properties?
      You can give them default value inside the overloaded init and that should be ok if that's what your use case demands but convenience init were made for that exact reason so why not just use them?
      As the documentation says
      "You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer’s parameters set to default values"
      Note: Don't ever create a class with 10 properties this was just for example hahaha Have a nice day Ram.

    • @ramgade2433
      @ramgade2433 4 роки тому +2

      @@CodeCat15 Thanks🙏 for the details. Appreciated the explanation

    • @CodeCat15
      @CodeCat15  4 роки тому +2

      Ram Gade no problem Ram, am happy to answer your doubts. Do share this channel with your friends or colleagues and ask them to subscribe as well I would appreciate the support.

    • @ramgade2433
      @ramgade2433 4 роки тому +1

      @@CodeCat15 Sure

    • @CodeCat15
      @CodeCat15  4 роки тому

      Ram Gade Thank you Ram, I appreciate that

  • @BabaBhaiya1215
    @BabaBhaiya1215 4 роки тому +1

    Nice explanation but there is no need to put self before properties if initializer parameter names are different :)

    • @CodeCat15
      @CodeCat15  4 роки тому +2

      That’s correct but then it’s always a good idea to value clarity and when you read a code which has a self we all know what that means and to which variable we are referring to because to be honest
      init(x:int) {
      x = x
      }
      Is less readable as compared to
      init(x:int){
      self.x = x
      }