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

Julien Schmitt
Julien Schmitt
2,334 Points

Classes - first challenge

Hi all,

I must admit that I'm getting through those Object Oriented courses and everything seems a little bit (not to say completely) confusing between structures, initializers, classes, declaring etc...

Trying to get through this first class challenge and the last task is to create an instance but I don't get what should be put in there as the variable numberOfSides has no determined value and if I assign Shape to my constant, Xcode highlights errors.

class Shape { var numberOfSides: Int init(numberOfSides: Int) { self.numberOfSides = numberOfSides } let someShape = Shape <-- I guess something should be added here but can't figure out what. }

Thanks for the help!

classes.swift
class Shape  {
    var numberOfSides: Int
    init(numberOfSides: Int) {
        self.numberOfSides = numberOfSides
    }
    }
   let someShape = Shape

1 Answer

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You're nearly there! You just need to instantiate someShape by using parentheses and putting in parentheses the same parameters that go into your init method.

Your init method looks like this:

init(numberOfSides: Int)

So your instantiation should look like this:

let someShape = Shape(numberOfSides: 3)

or whatever number of sides you want your shape to be.

I hope this helps!