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 Classes in Swift Instances of Classes

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

why does this not work?

why does numberOfSides not work?

objects.swift
// Enter your code below
class Shape {
    var numberOfSides: Int

    init(sides: Int) {
        self.numberOfSides = numberOfSides
    }
}

let someShape = Shape()

3 Answers

David Papandrew
David Papandrew
8,386 Points

It doesn't work because:

1) Your init method should assign the init method's sides parameter to self.numberOfSides (right now you are assigning an undeclared object, numberOfSides)

2) When creating the someShape constant, the init method should take an argument

Here's the corrected code:

// Enter your code below
class Shape {
    var numberOfSides: Int

    init(sides: Int) {
        self.numberOfSides = sides
    }
}

let someShape = Shape(sides: 4)
NIKOLA RUSEV
NIKOLA RUSEV
5,293 Points

hi david

i solve the challange but just want to ask you is it matter the name of argument in init? becaouse i solve with init(numberOfSides: Int) { self.numberOfSides = numberOfSides my question is not just for this challenge i mean further in codinig is this correct?

David Papandrew
David Papandrew
8,386 Points

Hi Nikola,

No the parameter name can be whatever you want. This is not a problem. Only issue is that the Treehouse challenges may want variables/constants and parameters with very specific names at times. In those cases, you will want to follow the instructions exactly or you might not pass the challenge (even though it will work in Playgrounds).