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

Jhoan Arango
Jhoan Arango
14,575 Points

Can someone please try to explain what is an “Instance”. I’ve done the research but, can’t seem to understand it fully.

I’ve done the research on this topic, but perhaps someone may be able to explain it better. I feel like I need to understand this so that I can continue.

2 Answers

Think of a class as a car factory and an instance as a car. The car factory produces cars and when you buy a car, you get a unique car. Of course, the car factory produces many copies of the same car, but it will still be unique (unique serial number, for example). This is just like when you ask for a new instance of a class. Whenever you do that, you get a unique instance of that class. The car might have attributes that you can change, like the color. This is just like instances, where you can set properties. You can also do stuff with your car, like wash it. Same thing with instances, you can send it methods.

Car *myCar = [Car new]; // the car factory produces a new car for you
myCar.color = blue; // you can change attributes
[myCar wash]; // you can do stuff to it.

Hope this clears it up a little. This example is based on the great Stephen Kochan and his book "Programming in Objective-C", which by the way is a great book on Objective-C.

Jhoan Arango
Jhoan Arango
14,575 Points

Well, it sure does helps me a lot. Thank you for the explanation, I'm very visual and that makes more sense to me now.

Here is an example that may help you to understand the general meaning of the term "instance". Consider converting a value to another type. It is never done implicitly. If you need to convert an Int to a String, say, you explicitly make an 'instance' of the desired type.

let sentence = "This string has a word count of "
let wordsInSentence = 7
let sentenceWordsDescription = sentence + String(wordsInString)     

This will result in: "this string has a word count of 7". By prefixing wordsInString with (String), we have made an instance of the desired type, in this case string.