What is the @interface section in a header file for?
I am wondering, what specifically @interface
section of a header file should hold?
From the examples i saw it seems that it is 开发者_高级运维used to declare instance variables. Accessors and other methods seem to be outside of it.
Please clarify
@interface
has at least two contexts: declaring a class and declaring a category. A class declaration looks like the following (with the bracketed stuff being optional):
@interface Class [: Superclass]
{
[fields]
}
[methods]
@end
A category declaration looks roughly the same, but has no fields zone:
@interface Class (CategoryName)
[methods]
@end
So, the @interface
line must be followed by braces in the case of a class declaration, and the fields of the class should be enclosed within. You put methods after the braces.
From the docs:
The interface file declares new classes that can be used by source code, holding all the information necessary to use the classes from other Objective-C code. Firstly, the file reveals to the programmer the position of the class in the class hierarchy by defining exactly which is the superclass. Secondly, it informs programmers of what variables are inherited when they create subclasses. Finally, the interface file may inform other software entities of the messages that can be sent to the class object and to the instances of the class.
精彩评论