Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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...

// 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,159 Points


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. :)