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

Justin Henderson
Justin Henderson
4,340 Points

I don’t seem to be getting the “class definition” portion of this challenge?

I have a good idea based on the Swift documentation but I’m not sure how to go about properly setting up my class definition & initializer method?

objects.swift
// Enter your code below
class Shape {
  var numberOfSides: Int

  init(){

  }
}

2 Answers

class Shape {
    var numberOfSides: Int

    init(sides: Int) {
        self.numberOfSides = sides
    }
}

let someShape = Shape(sides: 4)
Justin Henderson
Justin Henderson
4,340 Points

Kyle,

Thank you for the response this was very helpful. After going through it a few times I finally got it! Thanks again.

@ Justin Henderson what you've done is you've created a shape class which is correct but in the init method you've not set it up properly. What your meant to do is something like this

init(numberOfSides: Int){ self.numberOfSides: numberOfSides }

What you've done with your code is you've created an initialiser method that has done nothing with the numberOfSides property on the Shape Object and this will get you into an error, the way you've written it is such a way that your assuming the numberOfSides has a value already assigned to it which it hasn't. I hope you understand if any more help please ask.