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 trialDyna Diamond
Courses Plus Student 904 PointsPurpose | .m and .h files and their relation to one another: reason for .h file
Hello:
I wanted to clarify the relation of .h and .m files to one another?.
For example, we declare our properties -- which, if I am correct "declare/prepare/assign values" the objects we are going to use in the .m files? [Please correct if you could give a more direct and explicit explain at a higher level.]
If we are going to restate what we want/going to use in the .m, what is the purpose of the .h files?
- Organization
- Management
- Relation to code to execute in some order/proper methods?
- Instance variables [Explain needed]
3 Answers
Gareth Borcherds
9,372 PointsSo I would take a look at this answer over here: http://stackoverflow.com/questions/1449698/confusion-with-header-and-implementation-files-in-objective-c for a pretty detailed explanation on some of the things that go in each file. Also, the last paragraph of the answer explains some high level reasoning for the separation.
The way I like to think of it though is that the header file is like the user interface to your code. It tells you what your code is going to do and what your options are for interacting with it through various methods etc, but then the .m file is where the code is actually executed. Like the answer above states, it's difficult to appreciate when you are writing code that you aren't sharing with other people, but once you start implementing classes created by others, you'll appreciate the separation and it becomes easier to look at all the availability that a particular class is going to provide.
I'm no expert though, that's just how I have seen it in the stuff I've built thus far.
Tristan Gaebler
6,204 Points.h is where we declare properties and methods. .m is where we implement them. So basically, where were manipulate methods and properties. I like to think of it like this. Immmmmplementaion is .m, and the other is .h. Does that make sense?
Dyna Diamond
Courses Plus Student 904 PointsI understand that .m and .h files are. I was unsure the reason for the use of them together if you could, for example, declare properties and methods in the .m file. [Correct me if this incorrect].
I have become more clear on the use of them, but was looking for more theory to help understand and use these correctly.
.m - Implementation .h - header
From your answer, I understand that we can implement the methods that we declare in the .m file. However, I do remain unclear about what a property is [Are these considered values, etc]?
James Brown
2,095 PointsHi Dyna,
I'm also very new to this, but noticed one major benefit of having these two files. In a Sprite Kit game, when a spriteNode's .h file is imported into your SKScene, you can call a method in your SKScene that is implemented in the spriteNode's .m file. So all the methods that are only relevant to that spriteNode can be stored in the spriteNode .m file. Without the .h files acting as a bridge, all of your implementation methods would have to be in your SKScene which would be very difficult to keep organised.
Hope that helps.
James
Dyna Diamond
Courses Plus Student 904 PointsDyna Diamond
Courses Plus Student 904 PointsThank you for your answer, and did give me additional confirmation on the general idea that people look at these files. Thank you for the link, I am going to go read it now.
Thanks!