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

Jacob Horn
Jacob Horn
9,255 Points

Trouble finding the answer to this challenge

Hi all, I'm having an issue solving this code challenge. I feel like I understand these objects conceptually, but am having trouble working the instructions on this one.

Does this class need to have parameters? Are we setting an initial value for numberOfSides?

I feel like this may have been easier to solve if it had two tasks...

The attached code is just my most recent attempt, I've tried a few different setups (and will continue working on it). Thank you for your time!

classes.swift
// Enter your code below
class Shape(x: int)  {
  var numberOfSides: Int 

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

}

let someShape = Shape(x: 2)

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Jacob,

You're on the right track, but there are a couple of things.

First, when you declare a class it does not take any parameters. Second, the challenge wants the variable numberOfSides used, but you are using x in multiple places (not sure why).

So, with the exception of the parameters declared for a class, you code is essentially correct... just a 'naming' issue.

class Shape {
  var numberOfSides: Int 

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

let someShape = Shape(numberOfSides: 2)

Keep Coding! :)

Jacob Horn
Jacob Horn
9,255 Points

Thank you so much Jason! This was close to my initial attempt, I thought I was using the numberOfSides variable in too many places so I attempted to use x (not sure why either (: ), and wasn't sure if I could set an explicit initial value (2 in your case).