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

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)