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 2.0 Classes Instances of Classes

Valeria Ruiz
Valeria Ruiz
948 Points

classes

hi I am kind of lost in this. on the video example we had done SO much code previous to getting to classes and we called on many functions and methods that had been done in all those previous hours to complete our class. So right now I don't know how to objectively write this code further.

I already rematched the video, and this is as fas as I have come.

Please help :)

classes.swift
// Enter your code below
class Shape {
var numberOfSides: Int = 1

    init(x: Int, y:Int) {
   }
   numberOfSides -= factor
}

2 Answers

Your answer is pretty close to what they are looking for.

// first you declare the class of Shape
class Shape {

  // Then you need to set up the var properties
  // We set up the numberOfSides var to be of type Int
  var numberOfSides: Int

  // Remember that all Class definitions must have an init function
  // we are going to have one parameter passed into it for the numberOfSides which we want to be an Int
  init(numberOfSides: Int) {
    // We want to assign that argument to the numberOfSides property in our class
    self.numberOfSides = numberOfSides // we use self to reference the internal property
  } 
}

// then we instantiate the new someShape instance of our shape class
let someShape = Shape(numberOfSides: 4)

Let me know if you have any other questions.

Valeria Ruiz
Valeria Ruiz
948 Points

thanks so much Ryan! your explanation comments are priceless! Thanks so much!

Valeria Ruiz
Valeria Ruiz
948 Points

class Shape { var numberOfSides: Int = 1 } let someShape = Shape()

That could work but you would always have one side to your shape. Initializing with a parameter allows you to set the number of sides on each instance of shape that you create.