xcode 在同一文件中调用返回类型为“void”的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5615402/
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
Calling a method with return type "void" in same file
提问by rottendevice
I've got a simple question.
我有一个简单的问题。
In Objective-C, when you have a method you want to call, with a return type of void
, how you you call it from another method?
在 Objective-C 中,当你有一个方法要调用,返回类型为void
,你如何从另一个方法调用它?
The way I've been doing it in my application is this:
我在我的应用程序中这样做的方式是这样的:
[self nameOfMethod];
But that causes Xcode to spit out the following error:
但这会导致 Xcode 吐出以下错误:
Method '-nameOfMethod' not found (return type defaults to 'id')
Though it seems to still be executing.
虽然它似乎仍在执行。
Am I calling it right, or is there a better way?
我说得对,还是有更好的方法?
Thanks!
谢谢!
回答by
I'm guessing you haven't declared -nameOfMethod
in the class interface and you're calling it from another method whose implementation precedes the implementation of -nameOfMethod
, i.e.:
我猜你还没有-nameOfMethod
在类接口中声明,而是从另一个方法调用它,该方法的实现先于 的实现-nameOfMethod
,即:
- (void)someMethod {
[self nameOfMethod];
}
- (void)nameOfMethod {
// …
}
When the compiler is parsing -someMethod
and -nameOfMethod
hasn't been declared in the class interface, it generates a warning because it doesn't know about -nameOfMethod
yet.
当编译器正在解析-someMethod
并且-nameOfMethod
还没有在类接口中声明时,它会生成一个警告,因为它还不知道-nameOfMethod
。
There are essentially two solutions for this. You could reorder the implementation file so that -nameOfMethod
appears before -someMethod
, but that's not always possible. A better solution is to declare -nameOfMethod
in the class interface. If -nameOfMethod
is supposed to be called by clients of your class, place it in the corresponding header file. On the other hand, if -nameOfMethod
is only supposed to be called inside your implementation file, use a class extension. Supposing your class is named SomeClass
, this is what your header and implementation files would look like:
对此,基本上有两种解决方案。您可以重新排序实现文件,使其-nameOfMethod
出现在 之前-someMethod
,但这并不总是可行的。更好的解决方案是-nameOfMethod
在类接口中声明。如果-nameOfMethod
应该由您的类的客户端调用,请将其放在相应的头文件中。另一方面,如果-nameOfMethod
只应该在您的实现文件中调用,请使用类扩展。假设您的类名为SomeClass
,这就是您的头文件和实现文件的样子:
// SomeClass.h
@interface SomeClass : NSObject {
// … instance variables
}
// … external methods
- (void)someMethod;
@end
// SomeClass.m
#import "SomeClass.h"
@interface SomeClass () // this is a class extension
// … internal methods
- (void)nameOfMethod;
@end
@implementation SomeClass
- (void)someMethod {
[self nameOfMethod];
}
- (void)nameOfMethod {
// …
}
@end
Using class extensions, the order of method implementations won't matter.
使用类扩展,方法实现的顺序无关紧要。
回答by Roger
You need to make sure that your interface file contains a definition for nameOfMethod - so;
您需要确保您的接口文件包含 nameOfMethod 的定义 - 所以;
-(void) nameOfMethod;
回答by Michael Fredrickson
You're calling it correctly, but make sure that the interface for your (void) method is in your .h file.
您正确调用了它,但请确保您的 (void) 方法的接口在您的 .h 文件中。