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

Aditya Gawade
Aditya Gawade
1,182 Points

what is wrong? i did not understand the question. what is the procedure to create an instance?

Let's get in some practice creating a class. Declare a class named Shape with a variable property named numberOfSides of type Int.

Remember that with classes you are required to write an initializer method.

Once you have a class definition, create an instance and assign it to a constant named someShape.

classes.swift
// Enter your code below
class Shape () {
var numberOfSides : Int
let someShape = numberOfSides
}

4 Answers

Jhoan Arango
Jhoan Arango
14,547 Points

Initializing is the process of giving an object all it's properties an initial value. What do I mean by this ?

Let's say we want to create an object, in order for us to be able to create this object we need a "blue print", which sets the requirements for this object. This "blue print" we are going to call it a class.

Let's create a class that when fully initialized it will create a "Car", and this car will be the instance.

Example:

Here we have a class called AutoFactory, and this class has 3 properties that are optional they do not have any initial values other than "nil". As you can see the class has no initializer, so we can't give those properties any values at all so we won't be able to create an instance of this class.

class AutoFactory {

    // Properties
    let model: String?
    let color: String?
    let doors: Int?
}

let car = AutoFactory() // No initializer, so it won't create an instance.

Let's give this class an initializer:

class AutoFactory {

    // Properties
    let model: String?
    let color: String?
    let doors: Int?

    // Initializer
    init(model: String, color: String, doors: Int){
        self.model = model
        self.color = color
        self.doors = doors
    }

}

// Now we have an initializer and we can give those properties
// an initial value, and this will create an instance of the class. 
let fordFiesta = AutoFactory(model: "Fiesta", color: "White", doors: 4)

// You can access these properties with the dot notation.
fordFiesta.color // Will print white
fordFiesta.doors // Will print 4

Hope this helps you.

Gennaro Turco
Gennaro Turco
2,557 Points

This is the kind of help we need to properly learn! Thank you very much!!

Jhoan Arango
Jhoan Arango
14,547 Points

You are very welcome. You can tag me if you need help in other questions.

Happy Coding

Jhoan Arango
Jhoan Arango
14,547 Points

Hello:

So, the challenge wants you to create a class. After creating the class, you will need to create an instance of it.

Let's start by creating a class and providing it with an initializer.

class Shape {

    // Property
    var numberOfSides: Int?

    // Initializer
    init(numberOfSides: Int) {
        self.numberOfSides = numberOfSides
    }

}

The initializer is what gives properties their values.

After you have created the class, then you can have an instance of it.

// Instance of Shape. 
let square = Shape(numberOfSides: 4)

Good luck, if you don't understand please let me know.

Aditya Gawade
Aditya Gawade
1,182 Points

i am still having problems wrapping my head around the initializer concept. thank u for ur answer though

Jhoan Arango
Jhoan Arango
14,547 Points

Do you need a better explanation still ? sorry I am getting back to you now.

Aditya Gawade
Aditya Gawade
1,182 Points

no problem at all. yes i do if you don't mind please. i do not understand the initialiser concept. i feel like its useless.

Devin Boddie
Devin Boddie
6,777 Points

// This is the code that I used, but unfortunately it will not compile however I'm pretty sure that it is correct

class Shape {
    var numberOfSides: Int

    init(numberOfSides: Int) {
    self.numberOfSides = numberOfSides

    }
let someShape = Shape(numberOfSides: 3)
}
Jhoan Arango
Jhoan Arango
14,547 Points

Hello Davin:

What's happening with your code is that you are trying to create an instance of your Shape class within the class definition.

Try this:

class Shape { // This is the opening bracket of the class
    var numberOfSides: Int

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

} // This is the closing bracket of the class


// This is outside of the class, now you should be able to create an instance. 
let someShape = Shape(numberOfSides: 3)

Good luck