问题
Possible Duplicate:
Difference between @interface definition in .h and .m file
Obj C class files have two files .h and .m ,in which the .h holds interface definition (@interface) and .m holds its implementation (@implementation)
But i saw in some classes there is an @interface occurring in both .h and .m?
What is the need for the @interface in both files?Is there any specific reason to do so?And what are the advantages if did so?
回答1:
The @interface macro in the .m file is usually used for private iVars and properties for limited visibility. Of course this is completely optional but is undoubtedly good practice.
回答2:
The @interface
in the .h file is generally the public interface, this is the one that you would declare the inheritance in such as
@interface theMainInterface : NSObject
note the colons :
and then the super object that this @interface
is inheriting from NSObject
, I do believe that this can only be done in the .h file. You can also declare the @interface
with a category as well such as
@interface theMainInterface(MyNewCatergory)
So this means that you can have multiple @interface
s in one .h file like
@interface theMainInterface : NSObject
// What ever you want to declare can go in here.
@end
@interface theMainInterface(MyNewCategory)
// Lets declare some more in here.
@end
Declaring these types of @interface
s in the .h file generally makes everything declared in them public.
But you can declare private @interface
s in the .m file which will do one of three things it will privately extend the selected @interface
or add a new category to a selected @interface
or declare a new private @interface
You can do this by adding something like this to the .m file.
@interface theMainInterface()
// This is used to extend theMainInterface that we set in the .h file.
// This will use the same @implemenation
@end
@implemenation theMainInterface()
// The main implementation.
@end
@interface theMainInterface(SomeOtherNewCategory)
// This adds a new category to theMainInterface we will need another @implementation for this.
@end
@implementation theMainInterface(SomeOtherNewCategory)
// This is a private category added the .m file
@end
@interface theSecondInterface()
// This is a whole new @interface that we have created, this is private
@end
@implementation theSecondInterface()
// The private implementation to theSecondInterface
@end
These all work the same way, the only difference is that some are private
, some are public
and some have categories
I am unsure if you can inherit on an @interface
in the .m file.
Hope this helps.
回答3:
The @interface that appears in .m file is usually used for internal category definition. There will be a category name followed by the @interface statement in following format
@interface ClassName (CategoryName)
@end
When the category name is empty as following format, the properties and methods inside are considered as private.
@interface ClassName ()
@end
Also note that you may have a property declared as readwrite in private category and readonly in the header. The compiler will complain if both declarations are readwrite.
// .h file
@interface ClassName
@property (nonatomic, strong, readonly) id aProperty;
@end
// .m file
@interface ClassName()
@property (nonatomic, strong) id aProperty;
@end
回答4:
if we want to declare some private method then we use @interface declaration in .m file .
来源:https://stackoverflow.com/questions/12260729/class-files-of-obj-c-interface