Reputation: 117
Hi I'm studying IOS programing through example code from the web. I found that something is weird from this code because they already have h.file but they also have h.file code inside of the m.file as well.
@interface Manager : NSObject {
}
+(void) goMenu;
this is h. file
#import "Manager.h"
@interface Manager ()
+(void) go
+(void) wrap
@end
@implementation Manager
....
and this is beginning of m.file.
For beginner like me, this kind of situation making me really confusing. Please someone explain me what is happening?
Upvotes: 0
Views: 77
Reputation: 5060
It's a class extension.It can used to have private methods,instances also.Here is link which give you a little more details.
Upvotes: 0
Reputation: 6135
what you see in the .m file is a class extension. You can read more about class extensions and categories here
Upvotes: 1
Reputation: 5601
@interface Manager ()
in the implementation file is known as a class extension.
This is often used to add methods, properties etc. that the developer wants to keep private.
Upvotes: 2
Reputation: 10329
Interfaces within you .m file are seen as private and not shown when referencing you Manager
class in other classes.
However, I do believe you can call them from other classes as long as you don't add "Private" between ( and ). (making it @interface Manager (Private)
)... but you will get a warning that the class Manager might not implement such a method called wrap
.
Upvotes: 0
Reputation: 5157
All functions that are in the .h file are possibly interesting for other classes as well. These are the public interface that is visible for everyone. Other class files can #import the .h file and thus know all public functions.
The functions in the .m file on the other hand are the private interface. Until recently, all methods had to be declared before they are being used (also it can be handy to have a quick overview). By using this construct, developers declared the existence of the methods at the beginning of the .m file so that they can be used throughout the file. As .m files are not to be imported/included in other files, they are not externally visible per se.
Note though that this mechanism is not enforced by any kind of security mechanism and can be overridden if one chooses to.
Upvotes: 0
Reputation: 69499
The @interface Manager ()
in the.m
file is mostly used to declare private methods and properties.
Every thing declared in the .h are (mostly) public methods and properties. But sometimes you want to have methods and properties that aren't publicly visible.
Be aware that with objective-c you can still call these methods and properties, the compiler will give an warning about calling such a methods might nog work since the call might not responde to the methods.
Upvotes: 0