What's the rule of method declare in Obj-c? Could I use a method without declaration directly?
Asked Answered
S

2

6

In Xcode4.4, I found I can use a method define in .m file directly without declaration in .h head file or .m file.

what's the rule of method declaration in Obj-c? Could I use a method without declaration in head file?

Spangle answered 18/9, 2012 at 11:20 Comment(0)
S
3

If you declare a method in the header file, then other classes will be able to access that method.

Sandell answered 18/9, 2012 at 11:21 Comment(5)
And if you don't declare it in your header, other classes will not be able to use that method.Yorktown
But the class itself can always use that method.Menswear
If you declare the prototype in the .m file, you get a private method.Pickaninny
The compiler will compain (and, under ARC, fail), but you can send a message for an undeclared method, or use performSelector: and it will succeed at runtime.Guthrie
I don't declare the method neither in head file nor in .m fileSpangle
O
0

You import headers whenever you want to use the methods declared in the headers.

So if you have created a class called ObjectA then to be able to use the methods you have declared in ObjectA.h you need to import it #import "ObjectA.h".

You usually only need to do the imports in your implementation .m files. If you need it in your header file you can use the @class annotation like this:

@class ObjectA
...
@property (nonatomic, strong) ObjectA *objectA;
Oaxaca answered 18/9, 2012 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.