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

Can someone help explain the difference between Class, Instance, and Object, I am having trouble differentiating them.

The videos in this section keep talking about referring to them but I don't understand the difference between them and how they are used. Even the Apple guide's glossary definitions is just a giant circle that mentions the other two and not clearly explaining them or what they do. Its hard figuring out what Pasan is saying without understanding the difference.

1 Answer

Craig, think of a class as a pattern. The standard analogy is a cookie cutter. Just as a cookie cutter can be used to make cookies, so a class can be used to make objects, aka instances of the class.

Here's an example. You use a Circle class to create circle objects: Circle c1 = Circle(radius: 23). Here The type or class is Circle, the name of the Circle object is c1, the second Circle(23) is a call to the init() method, and that method takes one parameter, the circle's radius. If you do this Circle c2 = Circle(radius: 5) you have a second Circle object, or a second instance of the Circle class. The two objects have different memory locations, and different radiuses.

As you might have guessed, String is a class and can be used to create String objects. You can do this:

let x = String("xxx")
x.characters.count  //displays 3

Although there is a shortcut way of creating Strings: let x = "xxx", which does the same thing as above. In both cases a String object x is created. The first way explicitly calls the String init() method. The second calls it implicitly.

Note: later you will find that things get a bit more interesting with structs, enums, protocols, etc. But for now bottom line, is that an object is an instance of a class, and classes are used to create objects.