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.
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.
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.
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.
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
@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.
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.
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 }
Thank you for very good explanation 😊 keep it up
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 😊
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.
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.
Simple, Clear, Useful Explanation for Interview and work.
simple and clear. Thank you for sharing, Keep doing the Awesome work
I am glad the video was helpful, thank you Pratima and welcome to the channel
It was very helpful, kindly explain about required init as well. Thank you.
Really Good explanation.. Thanks alot...!!!
Hi Ravi , awesome video man, one question what shall we prefer giving default value in init or create convenience init and there give value
It depends on the use case as to what you are trying to achieve and do by providing those default values.
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.
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.
Very helpful video...Just add required init if possible so no need to search for required init video separately...
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
@@CodeCat15 ok sure👍🙌
It was really helpful :)
Awesome Prachi I am glad it was helpful
Very good Explanation and can you make a video covering Deinitializers in Swift
Hey Neeraj, checkout the video on ARC where i have covered deinit
Link: ua-cam.com/video/XAlXnApplk0/v-deo.html
Required init?
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
@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.
@@CodeCat15 Thanks🙏 for the details. Appreciated the explanation
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.
@@CodeCat15 Sure
Ram Gade Thank you Ram, I appreciate that
Nice explanation but there is no need to put self before properties if initializer parameter names are different :)
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
}