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

1,158 Points


Hey guys! Would be very glad if someone could explain me step by step how I should solve this task: "Let's get in some practice creating a class. Declare a class named Shape with a variable property named numberOfSides of type Int.

Remember that with classes you are required to write an initializer method.

Once you have a class definition, create an instance and assign it to a constant named someShape."

Thank you!

// Enter your code below

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

I would sincerely prefer to know what you've tried thus far that's failed so we can say what's going wrong. But here is my solution along with comments as explanations.

// Enter your code below
class Shape {       //declare the class Shape
  var numberOfSides: Int    //declare a variable to hold the number of sides of type int
  init (numberOfSides: Int) {   // initialize the number of sides of the shape
    self.numberOfSides = numberOfSides  // this sets the number of sides to the number of sides passed in by the creation of a new shape

let someShape = Shape(numberOfSides: 5)  //  this creates a new object of type Shape with 5 sides... aka a pentagon

Hope this helps, but if you have questions let me know! :smiley: