|
So, I am watching/reading Paul's view composition session https://www.hackingwithswift.com/books/ios-swiftui/view-composition He wrote this to demonstrate how to make small chunks of views and place them in they body view .
And, same thing happened in the session right after the previous one https://www.hackingwithswift.com/books/ios-swiftui/custom-modifiers Custom modifiers, they are also structs , but they are just directly placed in the body view without initializer. This is what Paul wrote:
Again, no initializer. My question:
|
|
|
|
Thank you Yes I know the idea of memberwise initializer, but it seems that everytime in our projects/challenges, Paul is always writing initializers, and it makes me nervous when I don't see one, becuase so many times Xcode warns me "there's no initializer". I am completely lost on when should use memberwise initializer and when to write them ourselves. (Theoritically I know, but when it comes to practice, it is still so confusing) Initiazlier is becoming my biggest nightmare. It is haunting me everywhere. |
|
Classes need you to write an initializer. You must tell classes, "When you create yourself, here are your initial values. But you must use this initializer to set everything up." (Unless you initialize all your vars with default values.) With Structs, you can just say "Create yourself, and here are your starting values." The struct will know how to assign values to its vars and lets without having a specific initializer. Not sure what lessons you are watching at the moment, but @twostraws gradually introduces more complex topics after you've been comfortable with the basics. So when you watch a video where he's creating an initializer for a Struct, ask yourself "WHY? Why is he creating an initializer??" Most likely you should be learning a new technique. Discover the reason behind the Struct's initializer, and take notes. In future, when designing your own Structs, you'll have several techniques to consider.
There is no ONE way to do this. You have to consider your struct's requirements, and select the correct technique. So if you examine the CapsuleText() struct, you'll see it requires just one parameter, a String. Do you need to write an initializer for this? Probably not. Use Technique #1, the free initializer. But when you get to Views that are customized components, perhaps containing a Slider, or some other control, you'll have to have bindings. These may require you to think about the struct's requirements. And no, the free initializer may not work for those views. This is when you'll want to refer to your excellent notes! |
|
You don't have to write an initializer for a class if you supply default values to all its properties. This is perfectly fine:
|
|
Every
then the complier does this automatically
However you can customize the
Note If you have a customize initializer you will lose the memberwise initializer However as @roosterboy says |
|
Thank you boys
Got it. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.