Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Classes in Swift Recap: Classes

Erik Martin
Erik Martin
4,901 Points

What exactly does "member-wise" mean as it pertains to initializers?

I understand how initializers work, but I don't know what it means by "member wise" initializer. What does the member wise part mean?

1 Answer

Bruce Röttgers
Bruce Röttgers
18,211 Points


this is the documentation site: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

This is the relevant section:

Memberwise Initializers for Structure Types Structure types automatically receive a memberwise initializer if they do not define any of their own custom initializers. Unlike a default initializer, the structure receives a memberwise initializer even if it has stored properties that do not have default values. The memberwise initializer is a shorthand way to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the memberwise initializer by name.

What you can conclude from this is that a memberwise initializer is one in which initial values are assigned to the instance. So, if you don't give the properties of a class, your code won't compile. In a structure, the memberwise initializer is automatically created for you. In classes you need to write it yourself like this:

init(myProperty: Type) {
    self.myProperty = myProperty

Hope that helps, Bruce.

Erik Martin
Erik Martin
4,901 Points

Thanks! This was really helpful :)