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! While you're at it, check out some resources Treehouse students have shared here.

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

Anthia Tillbury
Anthia Tillbury
3,388 Points

Member wise Initialiser definition correct?

I have this concept of what a Memberwise Initialiser is and would like confirmation please:

A Memberwise Initialiser Method is automatically generated by the compiler when creating an Instance of the Struct to assign values to each of its Stored Properties, thus Initialising it. e.g. let tired = EyeOpen(leftEye: ???, rightEye: ???)

3 Answers

Garrett Votaw
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Garrett Votaw
iOS Development Techdegree Graduate 15,223 Points

James,

You are correct. A member wise initializer essentially sets the values for all stored properties*

class Person {
   var name: String
   var age: Int

   init(name: String, age: Int) {               // <-- Member Wise Initializer
         self.name = name
         self.age = age
  }
}

Structs are unique in that the above initializer is written for you automatically so you can just call it when you setup your struct.

Here is some great info on initializers: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

Hope that helps! Happy coding!

*unless of course the stored properties are already set in which case they can be omitted from an initializer

Anthia Tillbury
Anthia Tillbury
3,388 Points

Thank you.

Just to clarify: a memberwise initialiser will be be used in both the "init" example you gave (when Initialising Properties) and the the example I gave; which would pass values into your Class from outside of it?

e.g.

class Person {
    var name: String
    var age: Int

    init(name: String, age: Int) {               // <-- Member Wise Initializer
        self.name = name
        self.age = age
    }
}

let averageJoe = Person.init(name: "Matthew", age: 47)            // <-- Member Wise Initialiser
Garrett Votaw
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Garrett Votaw
iOS Development Techdegree Graduate 15,223 Points

Yes basically.

Think of an init as a function.

class Person {
    var name: String
    var age: Int

    init(name: String, age: Int) {               // <-- Member Wise Initializer DEFINITION
        self.name = name
        self.age = age
    }
}

let averageJoe = Person.init(name: "Matthew", age: 47)   // <-- Member Wise Initializer Call

It sounds like you got it down though

Anthia Tillbury
Anthia Tillbury
3,388 Points

Thanks for your help, I'm not the academic type so this stuff is quite hard to understand.

Learning a new "language" I don't want it to be like English; where I can speak it well enough, but I don't understand the rules of the syntax. I'm sure that I could learn to code, but I'll never be great if I don't understand how to explain what I am doing.