xcode 不能在头文件中声明方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8839854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we not declare methods in the header files?
提问by user1146115
I am watching the Stanford University iPad and iPhone application Developments course video. The instructor says in the video we can control-drag an UI object to the implementation files to create an action. But in this way the method will not declare in the header file. Does this mean it is ok to implement methods in the .m file but not declare in the .h file?
我正在观看斯坦福大学 iPad 和 iPhone 应用程序开发课程视频。讲师在视频中说我们可以控制拖动一个 UI 对象到实现文件来创建一个动作。但是这种方法不会在头文件中声明。这是否意味着可以在 .m 文件中实现方法但不在 .h 文件中声明?
采纳答案by CRD
Depends on how you define "ok" :-)
取决于你如何定义“好的”:-)
Objective-C uses dynamic method lookup and does not really enforce access ("private", "public", etc.) specifiers. So you don't needto declare any method in a header file.
Objective-C 使用动态方法查找,并没有真正强制执行访问(“私有”、“公共”等)说明符。所以,你没有需要申报的任何方法在头文件。
Howeveryou will end up fighting the compiler as it does do a fair amount of type-checking unless you persuade it not to, and you'll lose by doing so.
但是,除非您说服它不这样做,否则您最终将与编译器作斗争,因为它确实进行了大量的类型检查,否则您将失败。
回答by Hot Licks
You are not required to declare in the header file all methods in the implementation. But if not in the header file obviously you cannot reference them by literal name in another file, nor can you "forward reference" them in the implementation file.
您不需要在头文件中声明实现中的所有方法。但是如果不是在头文件中,显然你不能在另一个文件中通过文字名称引用它们,也不能在实现文件中“向前引用”它们。
(Note that this is not that different from regular C, but is different from methods of a class in C++.)
(请注意,这与常规 C 没有什么不同,但与 C++ 中的类的方法不同。)
回答by mattjgalloway
It's "OK" to not declare methods in the header yes, under certain circumstances. For instance, if using ARC then the compiler generally needs to know the method signature so it can do the right thing. But basically all it means is that wherever you're using the method, it must already know about the method you're calling.
在某些情况下,不在标头中声明方法是“OK”的。例如,如果使用 ARC,那么编译器通常需要知道方法签名,以便它可以做正确的事情。但基本上这意味着无论您在何处使用该方法,它都必须已经知道您正在调用的方法。
Since you're talking about Interface Builder, that's slightly different in that it will know about all methods since it can "see" the whole context of your header and implementation files and know that a method exists. i.e. in my terminology above, the method has been defined before it's used.
由于您在谈论 Interface Builder,因此它会了解所有方法,因为它可以“查看”头文件和实现文件的整个上下文并知道方法存在,因此略有不同。即在我上面的术语中,该方法在使用之前已经定义。
With regard to defining before use, the general accepted approach is to either:
关于使用前定义,普遍接受的方法是:
Define a method in the interface file (.h). e.g.:
MyClass.h
@interface MyClass : NSObject - (void)someMethod; @end
MyClass.m
@implementation MyClass - (void)someMethod { // do something } @end
Define a method in a class continuation category. e.g.:
MyClass.h
@interface MyClass : NSObject @end
MyClass.m
@interface MyClass () - (void)someMethod; @end @implementation MyClass - (void)someMethod { // do something } @end
在接口文件 (.h) 中定义方法。例如:
我的类.h
@interface MyClass : NSObject - (void)someMethod; @end
我的课堂.m
@implementation MyClass - (void)someMethod { // do something } @end
在类延续类别中定义方法。例如:
我的类.h
@interface MyClass : NSObject @end
我的课堂.m
@interface MyClass () - (void)someMethod; @end @implementation MyClass - (void)someMethod { // do something } @end