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 Classes in Swift Instances of Classes

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

still having trouble with instances.

I am still having trouble understanding instances can someone explain them to me and help me with this challenge.

objects.swift
// Enter your code below
class Shape {
    var numberOfSides: Int
    let someshape

    init(shape: String) {
        self.someShape = someshape
    }
}

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

an instance is an object of the class, so in this case a shape would be an instance of the shape class. square, triangle, whatever it is will be an instance of the shape class. if we made a bank account class, each account we created would be an instance of the bank account class. if we had a toy class, each toy would be an instance. the first two lines of your code are ok but the rest need some work. first, the instance you are going to store in someshape needs to be created outside of the class definition, so below everything you've got right now. for your init method, notice that the only variable each shape has is number of sides, so that will be the parameter in the init definition and will be what is set in the init body. someshape is not an instance variable and is not set in the init body. now, outside of the class definition, we create an instance of shape and assign it to someshape. so after the = sign call shape and give it an integer argument in the (). you have to include the variable name, number of sides, when passing in the argument. be sure to capitalize everything like they do or it may throw an error.