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

NATHAN TRAN
NATHAN TRAN
2,367 Points

Instance

I have my class and variable, I think i didn't write out my initializer correctly.

objects.swift
// Enter your code below

class Shape {
    var numberOfSides: Int

    init(){

        let someShape: numberOfSides

    }


}

2 Answers

Matt Skelton
Matt Skelton
4,548 Points

Hey Nathan,

Good start, but you're spot on, your init method is a little bit off.

You want to add a parameter to your init method so that you can customise the number of sides that any instance of shape will have. Using the value that is passed into this parameter, you need to assign it to the numberOfSides variable that you have created within your class.

Once you have created your init method, you then need to create an instance of shape and assign it to a new constant named newShape. Whilst doing this, you will have to pass in an integer value in order to satisfy the init method you have just created.

class Person
{
    var age: Int

    init(age: Int)
    {
        self.age = age
    }
}

let somePerson = Person(age: 25)

I've provided a very similar example above that will hopefully illustrate what I've described. I hope this helps, let me know if you need any further pointers!