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

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!