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 Object-Oriented Swift 2.0 Classes Instances of Classes

Ryan Maneo
Ryan Maneo
4,342 Points

Help. Please!!!

My specific question is below. But I need some serious feedback. I am EXTREMELY frustrated trying to learn this. I have the syntax memorized pretty much, but what to do with it is a constant struggle. I often find myself coming here because of lack of detail in questions. I get the final conclusion, but there are probably several ways to solve this...

classes.swift
// Enter your code below


class Shape {

var numberOfSides: Int


init//(What values do I put here? I was never told what to put...) 
{

definition = "This is a definition I guess? No specifics here? So I can write whatever right?"
}
self. // What do I Put here???
let someShape = // Assign "It," what is "It?"
}

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

Ryan,

You are thinking along the right lines. Take a look at your code modified below with my comments inline.

// Enter your code below
class Shape {
    var numberOfSides: Int  // You did right with the property here.

    init(numberOfSides sides: Int) {  // Here we take in the numberOfSides when we initialize the object.
        // Note that I am using an external parameter name of numberOfSides but refer to it as sides inside my code.
        self.numberOfSides = sides  // Here I am setting that property for my object to be what I passed in.
    }
} // We close the class definition now and declare the object instance below in the global scope.

let someShape = Shape(numberOfSides: 4)  // Here I am initializing my Shape object passing in the int 4 for sides.

I hope this helps clear it up for you. You are doing a great job. Keep up the good work. It does get easier, I promise. :)