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