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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:46:33  来源:igfitidea点击:

Calling a method with return type "void" in same file

iphoneobjective-cxcode

提问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 -nameOfMethodin 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 -someMethodand -nameOfMethodhasn't been declared in the class interface, it generates a warning because it doesn't know about -nameOfMethodyet.

当编译器正在解析-someMethod并且-nameOfMethod还没有在类接口中声明时,它会生成一个警告,因为它还不知道-nameOfMethod

There are essentially two solutions for this. You could reorder the implementation file so that -nameOfMethodappears before -someMethod, but that's not always possible. A better solution is to declare -nameOfMethodin the class interface. If -nameOfMethodis supposed to be called by clients of your class, place it in the corresponding header file. On the other hand, if -nameOfMethodis 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 文件中。