When I create new classes in x-code it breaks it into a .h and a .m file. can I ignore the .h file and implement the entire class inside the .m file? If so what are the downsides?
Asked
Active
Viewed 100 times
0
-
From what I understand, which is limited with Obj-C, it is required to use the .h files to control the access modifier of certain class properties, amongst other things I am sure. So in short, yes. Perhaps someone who knows obj-c can elaborate. – GCaiazzo Dec 19 '11 at 03:37
3 Answers
7
It is absolutely possible to implement the whole class in a single .m file just as it is possible to implement an entire c++ class in a .cc file. The major disadvantage is that you lose the decoupling of interface and implementation. Another major disadvantage is that you will need to repeat the same interface code in any other .m file that utilizes the class. In other words you lose the ability to simply import the .h file in any other class file.

ennuikiller
- 46,381
- 14
- 112
- 137
1
Yes it's possible. Sometimes I find it handy to have the @interface
and the @implementation
in one .m file to keep the class more or less private or hidden from other classes.

Jessedc
- 12,320
- 3
- 50
- 63
1
Yes, possible. Often done for "internal" classes that you don't want to expose.

Mugunth
- 14,461
- 15
- 66
- 94