xcode 未找到实例方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12124034/
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-15 01:19:59  来源:igfitidea点击:

Instance Method not found

objective-cxcodecocos2d-iphone

提问by Ryuk

I am stumped, im not very good with Objective-C yet, and I have been following tutorials until now.

我被难住了,我对 Objective-C 还不是很擅长,直到现在我一直在学习教程。

I have decided to add all the functions I use in multipul views in one "CommonClass" I have called GeneralHelper.

我决定将我在多视图中使用的所有函数添加到一个我称为 GeneralHelper 的“CommonClass”中。

When trying to call my function im getting the following error:

尝试调用我的函数时,我收到以下错误:

Instance method'-convertPoint:' not found(return type defaults to 'id')

实例方法'-convertPoint:'未找到(返回类型默认为'id')

Below is my code:

下面是我的代码:

My GeneralHelper.h lookslike this: #import

我的 GeneralHelper.h 看起来像这样:#import

    @interface GeneralHelper : NSObject

    @end

    GeneralHelper* gHelper;

My GeneralHelper.m looks like this:

我的 GeneralHelper.m 看起来像这样:

    #import "GeneralHelper.h"
    #import "WhackGame.h"
    #import "SimpleAudioEngine.h"
    #import "Settings.h"

    @implementation GeneralHelper
    -(CGPoint)convertPoint:(CGPoint)point {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            return ccp(32 + point.x*2, 64 + point.y*2);
        } else {
            return point;
        }
    }

    @end

Inside my Initialize method I have:

在我的 Initialize 方法中,我有:

    gHelper = [[GeneralHelper alloc] init]; //First, we create an instance of GeneralHelper

and im trying to call it like:

我试图这样称呼它:

    mole1.position = [gHelper convertPoint:ccp(85, 85)];

Any ideas?

有任何想法吗?

Thanks

谢谢

采纳答案by DrummerB

You didn't declare the method convertPoint:in your @interfacelike this:

你没有申报的方法,convertPoint:在你@interface这样的:

@interface GeneralHelper : NSObject
- (CGPoint)convertPoint:(CGPoint)point;
@end

Also, if you're trying to have one shared instance of GeneralHelper, that's not the way to do it. Instead you should have a class method that returns an instance.

此外,如果您想拥有一个 GeneralHelper 的共享实例,那不是这样做的方法。相反,您应该有一个返回实例的类方法。

@interface GeneralHelper : NSObject
- (CGPoint)convertPoint:(CGPoint)point;
+ (GeneralHelper *)sharedHelper;
@end

Then in your implementation:

然后在你的实现中:

+ (GeneralHelper *)sharedHelper
{
    static GeneralHelper *sharedHelper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedHelper = [[GeneralHelper alloc] init];
    });
    return sharedHelper;
}

And then wherever you need it:

然后在您需要的任何地方:

CGPoint convertedPoint = [[GeneralHelper sharedHelper] convertPoint:point];

Alternatively you could simply use class methods:

或者,您可以简单地使用类方法:

@interface GeneralHelper : NSObject
+ (CGPoint)convertPoint:(CGPoint)point;
@end

The implementation is the same as before, but with a + instead of a - sign. In this case you don't need the sharedHelpermethod and can simply call it like this:

实现与以前相同,但使用 + 而不是 - 符号。在这种情况下,您不需要该sharedHelper方法,只需像这样调用它:

CGPoint convertedPoint = [GeneralHelper convertPoint:point];

回答by John Calsbeek

You need to declare your convertPoint:method in your class's interface.

您需要convertPoint:在类的接口中声明您的方法。

@interface GeneralHelper : NSObject
- (CGPoint)convertPoint:(CGPoint)point;
@end

A header acts as "here is all the information you need to use this class," and one important piece of information is "here are the messages that you can send to this class, and what arguments they take."

标题充当“这里是您使用该类所需的所有信息”,其中一条重要信息是“这里是您可以发送给该类的消息,以及它们采用的参数”。

Your specific warning is caused by the compiler being unable to figure out the return type of the method without seeing it in the interface.

您的具体警告是由于编译器无法在未在界面中看到该方法的返回类型的情况下确定该方法的返回类型而引起的。