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

Benjamin Lim
Benjamin Lim
2,747 Points

How were we suppose to come to this conclusion with the information we've been given?

This is the correct answer below. I copied it from the forum, but i am absolutely stumped on how we were suppose to come to this conclusion with the information given? I re-watched the previous videos several times whilst concurrently referring to the apple Swift Programming Language book to try to pick up what to do here. I didn't even know we had to run the variable through the initialiser (did i miss this in the video? if so, please do tell where i can find it?). Furthermore, i didn't know we had to assign an integer value to run the code. Is it me? Or is this very demanding for an introduction. I completely understand that this is about problem solving, but i feel like I haven't received adequate guidance in recent lessons to perform the challenge tasks with confidence. Most of the challenge tasks before object oriented swift were very much possible but I've recently felt very lost and uninformed.

classes.swift
// Enter your code below
class Shape{
  var numberOfSides: Int

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

let someShape = Shape(sides: 3)
Benjamin Lim
Benjamin Lim
2,747 Points

This is what it says: 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.

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Benjamin:

I am sorry you feel that way, it can get overwhelming when you are learning a new language, specially a coding language. I went back to the challenge and the videos prior to the challenge, and I do find the information that teaches you how to declare a class and what is a "variable property".

Learning a language, means you have to learn a few things other than just writing code. Terminology is very important, perhaps one of the most important.

I will break down the challenge for you, and you will see that it is not as difficult.

Challenge

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

The first step is telling us that we have to declare a class that has a name of "Shape", with a variable that has a name "numberOfSides" of the type Int.

// Let's declare that class

class Shape {
// Lets add that variable

var numberOfSide: Int

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

Then it reminds us that classes need an initializer. Remember structs generate their own initializer, which are called memberwise initializer. On the other hand, classes requires you to create an initializer for it.

class Shape {
var numberOfSide: Int

// Lets create the initializer

init(numberOfSides: Int){
       self.numberOfSides = numberOfSides
     }
}
  • Once you have a class definition, create an instance and assign it to a constant named someShape.

Now that we have a class declared, they want us to create an instance of this class, and then assign it to a constant named "someShape"

class Shape {
var numberOfSide: Int

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

// Here we will create that instance and assign it to the constant.

let someShape = Shape(4)

/*
You may ask, why in the answer I found it has 
Shape(sides:4) and you have Shape(4), and that's because 
I did not give the parameters it's external parameter name. On your
example it has the external parameter name, and the local parameter name.
*/

I am sure that when you look at it this way, you will realize that it was not as difficult and that somewhere in those videos you did see this information given to you. But it will not hurt if you go back to re-watch some of the videos. I am not sure how far you are along with this course, or if you started from "Swift 2.0 basics" and continued the courses by levels. If you have not done that, then I recommend you do so.

If you have any other questions, or do not understand my answer, please let me know..

Good luck

Benjamin Lim
Benjamin Lim
2,747 Points

I really appreciate you breaking it down for me Jhoan! I did in fact start from Swift 2.0 basics, and found everything up till object oriented programming to be very well guided. I guess at this point, it's figuring out how to problem solve whilst simultaneously tapping into everything I've learned so far to do so. I think the way you've broken it down for me has made it a lot easier for me to digest and register it. Thank you for your help and I hope you haven't taken my frustration as a sweeping statement but a genuine inquiry on how to improve myself.