In objective c, what actually happened when you say #import "MyClass.h"? (Is the compiler copying something for you?)
In MyClass.m file, if I #import "UsefulClass.h"
, this means that UsefulClass
becomes available under this file and I can create object and send message to an instance of it.
In myClass.m file, I have to #import "MyClass.h"
, this sounds like linking my implementation file to its header (called base file?), which feels quite different from what the first one does.
So does #import
do two different kind of things based on circumstances? Or does it actually fall into one category from another perspective.
The method defined in .m file but not in .h file is considered private. From another class, can I invoke the private method somehow? (like if I #import .m instead of .h? so the class will know about what the implementation file defines.)
What is the difference between objective-c #import
and c #include
?
Also @interface MyClass : NSObject
is used in .h file and @interface MyClass()
is used in the .m file. So is it just a syntax format(like the bracket) when you want to have private properties? or is there any logic behind this?
Is the compiler copying something for you? [when you say
#import "myClass.h"
]?
No, it does not. An #import
preprocessor directive is nearly identical to the #include
directive, with the exception that it does not need an inclusion guard - a construct borrowed from C, which does not look intuitive.
#import
a .h header into a .m file is to imagine that the content of that header is placed in its entirety on the line replacing the #import
. Essentially, that is what the preprocessor does.#import
.