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
Joseph Lau
5,420 PointsObjective-C: Classes vs Objects
Coming from a little bit of a JavaScript background, I have a hard time understanding classes in Objective-C. In JavaScript, an object contains properties, values, and methods. I'm having a hard time relating these concepts to Objective-C.
I understand that classes are like blueprints to objects, like a blueprint to a house object that contains properties like a "toilet", values like "white", and methods like "flush". But in terms of Objective-C, a class is split into header and implementation files, header file being the property and method declarations(?) used for interacting with the rest of the code in the main file and implementation file for the actual values of those properties and code for the methods. How close am I?
Also, where do I draw the line between calling something a class and calling something an object? When is something called a function and when is it called a method?
2 Answers
Stone Preston
42,016 Pointsyes your understanding is correct. a class is a blueprint of an object, while an object is an instance of that class. Its really no different from javascript. the OOP concepts are the same
the header file contains the declarations, while the implementation file contains the actual implementation of what you declared in the header.
an object becomes an object when it it allocated and initialized.
if you were writing code in your someViewController.m file, you would say you were coding your someViewController class. if you then created an instance of that someViewController class somewhere, you would say you had just created a someViewController object
methods and functions are similar. methods are associated with an object meaning it has access to that objects properties, other methods, etc and you can pass arguments to it as well. A function is not associated with an object is really just a piece of code you can reference and pass arguments to.
Joseph Lau
5,420 PointsThat makes sense, thanks!