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

Theo VOGLIMACCI
Theo VOGLIMACCI
8,027 Points

Can't finish this challenge

Hello,

Please could you explain me in details how to finish this challenge?

My class seems good, but i don't know how to make the init method in it.

objects.swift
class Shape {
    var numberOfSides: Int = 0

    init() {
         self.numberOfSides = numberOfSides
    }

}

let someShape = Shape

2 Answers

Theo VOGLIMACCI,

As you pointed, there is a problem in your init method. If you look closely, you are trying to assign self.numberOfSides = numberOfSides. But where is the numberOfSides coming from? Remember, you should get this as an argument. For example:

class Person {
   let firstName: String
   let lastName: String

   init(firstName: String, lastName: String) {
       self.firstName = firstName  // I am getting this firstName from the argument above
       self.lastName = lastName   // Same thing here.
   }
}

I made an example with a different class just to stimulate your brain, but if you still stuck please let me know.

-Dan

Theo VOGLIMACCI
Theo VOGLIMACCI
8,027 Points

Well much thanks Daniel,

Here is the full answer if someone needs help aswell.

// Enter your code below

class Shape {
  var numberOfSides : Int

  init(numberOfSides: Int) { //as you mentioned I forgot to declare NumberOfSides as an argument in the init method
  self.numberOfSides = numberOfSides 
  }
}

let someShape = Shape(numberOfSides: 4)

You are welcome!