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

.self - I don't understand what it's doing in my code?

Here I have some code where I found that .self was necessary, but even though I understand it to be used "when distinguishing between parameters or argument labels and property names" I cannot tell what's going on in my own code:

struct EyesOpen {
    var leftEye, rightEye: Bool
    var middleEye: Bool = true

    init(leftEye left: Bool, rightEye right: Bool) {
        self.leftEye  = true
        self.rightEye = true        // self ??
    }
    init(eyesClosed closed: Bool) {
        leftEye  = false
        rightEye = false
    }
    init(_ halfAwake: Bool) {
        leftEye  = true
        rightEye = false
    }

}

let tired = EyesOpen(leftEye: true, rightEye: true)

I know the code isn't efficient, and it may not make sense, but I was practicing Initialisation, and came out with this.

What I don't understand is why I need to reference "leftEye" to the Property value in Struct, if that's what I'm doing?

Thanks!

4 Answers

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

Hey James,

.self is used when you are inside an object and you need to reference itself or reference a property within the object you are referencing.

For instance

I'm Garrett and I have an ice-cream cone. In English I simply say that's "MY ice-cream cone" but in swift we write "self.iceCreamCone".

struct Garrett {
  var iceCreamConeProperty: String


  init (iceCreamConeParam: String) {
     self.iceCreamConeProperty = iceCreamConeParam      //<-- Here I am saying "set Garretts Ice cream equal to whatever i pass into this initializer"
                                                        // I named them differently to intentionally give you an idea of which is which.
  }
}

Hope that kind of helps! Happy Coding!

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi James - Possibly one of the most important tools as a programmer is being able to locate information for what you are trying to use or understand. No programmer knows every method, function, library, etc. At my job we keep the documentation open at all times. I have provided a link to the swift documentation on initialization; I believe you will be able to answer your own question by going through this.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203

Anthia Tillbury
Anthia Tillbury
3,388 Points

I've been tackling this now, from various sources, for a few days. How about this:

.self refers to the Instance values passed in, overriding the Parameters

Jeff McDivitt
Jeff McDivitt
23,970 Points

Garrett Votaw answered it and explained perfectly for you

Anthia Tillbury
Anthia Tillbury
3,388 Points

Hey, that's pretty good!

So if I set the value at the time of declaration it would still ignore it and keep an eye out for whatever I put in via the Initialiser:

struct Garrett {
    var iceCreamConeProperty: Int = 88


    init(iceCreamConeParam: Int) {
        self.iceCreamConeProperty = iceCreamConeParam
    }
}


let iceCone = Garrett.init(iceCreamConeParam: 11)

iceCone.iceCreamConeProperty