Why are objective-c @interface definitions placed in a seperate .h file?
I recognize that this is a general type of question but I'd be interested in your opinion on:
- why are @interface @protocol and @property definitons seperated into a .h header file and then imported 开发者_JAVA百科into the implementation file?
Is there anybody out there who just defines everything in a .m file?
BTW> its not that I am planning on not using .h files, just trying to understand the thinking behind it!
Because Objective-C relies on standard C infrastructure in which every compilation unit (.m
) is compiled separately and then all of them are linked together.
This means that the static type checking phase of the Objective-C compiler will need just the .h
files to know the characteristics of declared classes (eg. signatures of methods) but just to ensure that everything is used as it is supposed to be, and this is why you #import
them in other source files that require what you declared inside the header.
if you have A.h/.m
and B.h/.m
which uses A you can think of the header file like a contract: "if you want to use A, I will link its binary code at the end of the compilation phase, what A is able to provide is described in this header file and this is the only thing you should know"
精彩评论