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

Kawal Oberoi
Kawal Oberoi
6,429 Points

Where did I got wrong. OMG!! ; - ;

Please help! ; - ;

classes.swift
// Enter your code below

class Shape(){
var numberOfSides: Int
init(numberOfSides: Int){
self.numberOfSides = numberOfSides
}
}

let someShape = Shape()

I'm having the same problem. What's wrong with my code?

class Shape { 

    var numberofSides: Int 

 init() {
    self.numberOfSides = numberOfSides 
          } 
                 } 

let someShape = Shape()
Greg Kaleka
Greg Kaleka
39,018 Points

sugabelly, you've got a capitalization error on the numberOfSides property. Tiny typos can cause cause big headaches! :confounded:

1 Answer

Greg Kaleka
Greg Kaleka
39,018 Points

Hi Kawal,

Two small issues with your code:

  1. When declaring a class, you don't need opening and closing parentheses. This isn't a function, and we're not creating a class instance.
  2. When you do create an instance of Shape, you have to use the initializer you just created, which takes one argument - an integer for the number of sides.

I think you can take it from here!

Cheers :beers:

-Greg

Kawal Oberoi
Kawal Oberoi
6,429 Points

Fixed! Yay!

But Seriously man, Object Oriented Programming is pretty confusing. Especially when we are declaring so many values etc. There should be some analogy to make it easier.

Greg Kaleka
Greg Kaleka
39,018 Points

It's hard to wrap your head around, for sure! You'll get it though. Keep working on it!