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

trouble creating instance of class

Seems I'm not properly creating an instance of class. Or maybe something else is wrong, but I have no usable errors in the preview. any pointers?

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

2 Answers

Hi Andrew,

You're correct that no errors are showing up inside the preview window. I'm not sure why you aren't receiving errors, but I can help you with passing the challenge.

In order to successfully create an instance of the class, you need to 1) declare the class, then 2) create an instance of the class outside of the class declaration.

Currently, it looks like you are attempting to create an instance of the class Shape in line 6 by writing the following:

let someShape = Shape.self

The first issue: You cannot create an instance of a class from within the class declaration. Solution: Move line 6 outside of your class, placing it after the class declaration.

The second issue: The syntax you are using to create an instance of class Shape is incorrect. Shape has a parameter called numberOfSides. In order to create an instance of class shape, you need to pass in an argument for number of sides. Your syntax should look like this:

let someShape = Shape(numberOfSides: 4)

Hope this helps.

I wrote the following. Is this wrong? It passed...

class Shape {
    var numberOfSides: Int = 2

    init(numberOfSides: Int){

    }

}

let someShape = Shape(numberOfSides: 2)