Introduction to Swift: Initializers (init method) part 2

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ก.ย. 2024
  • In this session, we shall have a look at what is designated init and convenience init methods in swift.
    Do share your thoughts on this video and don't forget to leave a comment, share and subscribe to show your love to this channel
    Connect with me on social media
    Email: codecat15@gmail.com
    Twitter: / codecat15
    Facebook: / code.cat.792
    Github: github.com/cod...
    Instagram: / codecat15

ความคิดเห็น • 32

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

  • @vyankatesha.shivnikar696
    @vyankatesha.shivnikar696 7 หลายเดือนก่อน +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  7 หลายเดือนก่อน

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

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

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

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

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

  • @vyankateshshivnikar7932
    @vyankateshshivnikar7932 2 ปีที่แล้ว

    Required init?

  • @neerajgupta8241
    @neerajgupta8241 3 ปีที่แล้ว +1

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

    • @CodeCat15
      @CodeCat15  3 ปีที่แล้ว

      Hey Neeraj, checkout the video on ARC where i have covered deinit
      Link: th-cam.com/video/XAlXnApplk0/w-d-xo.html

  • @naveenkumardugarbbachhrajd9682
    @naveenkumardugarbbachhrajd9682 ปีที่แล้ว +1

    Amazing one .... your all videos worth time spending and too too easy to understand the concept

    • @CodeCat15
      @CodeCat15  ปีที่แล้ว

      Glad to hear that, and do share the channel with your iOS group and feel free to ask questions if any

    • @naveenkumardugarbbachhrajd9682
      @naveenkumardugarbbachhrajd9682 ปีที่แล้ว

      @@CodeCat15 please can you explain how to connect my sqlite on latest version of xcode and swift , how to create and connect a database . Thankyou .

  • @abhijnanmaji2682
    @abhijnanmaji2682 2 ปีที่แล้ว

    Simple, Clear, Useful Explanation for Interview and work.

  • @abhayshukle3693
    @abhayshukle3693 2 ปีที่แล้ว

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

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

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

  • @ganeshdeore5557
    @ganeshdeore5557 3 ปีที่แล้ว

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

  • @prachibile2798
    @prachibile2798 3 ปีที่แล้ว +1

    It was really helpful :)

    • @CodeCat15
      @CodeCat15  3 ปีที่แล้ว

      Awesome Prachi I am glad it was helpful