ios 接收者类型 *** 例如消息是前向声明

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

receiver type *** for instance message is a forward declaration

iphoneiosobjective-cforward-declaration

提问by SentineL

In my iOS5 app, I have NSObjectStatesclass, and trying to init it:

在我的 iOS5 应用程序中,我有NSObjectStates课程,并尝试初始化它:

states = [states init];

here is initmethod in States:

这是init方法States

- (id) init
{
    if ((self = [super init]))
    {
        pickedGlasses = 0;
    }

    return self;
}

But there is error in the line states = [states init];

但行中有错误 states = [states init];

receiver type "States" for instance message is a forward declaration

接收器类型“状态”例如消息是前向声明

What does it mean? What am I doing wrong?

这是什么意思?我究竟做错了什么?

回答by Catfish_Man

That basically means that you need to import the .h file containing the declaration of States.

这基本上意味着您需要导入包含状态声明的 .h 文件。

However, there is a lotof other stuff wrong with your code.

但是,您的代码还有很多其他问题。

  • You're -init'ing an object without +alloc'ing it. That won't work
  • You're declaring an object as a non-pointer type, that won't work either
  • You're not calling [super init]in -init.
  • You've declared the class using @classin the header, but never imported the class.
  • 您正在初始化一个对象而不对其进行初始化+alloc。那行不通
  • 您将对象声明为非指针类型,这也不起作用
  • 你不是[super init]在打电话-init
  • 您已@class在标头中声明了该类,但从未导入该类。

回答by capikaw

FWIW, I got this error when I was implementing core data in to an existing project. It turned out I forgot to link CoreData.h to my project. I had already added the CoreData framework to my project but solved the issue by linking to the framework in my pre-compiled header just like Apple's templates do:

FWIW,当我在现有项目中实施核心数据时遇到此错误。结果我忘了把 CoreData.h 链接到我的项目。我已经将 CoreData 框架添加到我的项目中,但是通过链接到我的预编译头文件中的框架解决了这个问题,就像 Apple 的模板一样:

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
#endif

回答by Stunner

I got this sort of message when I had two files that depended on each other. The tricky thing here is that you'll get a circular reference if you just try to import each other (class A imports class B, class B imports class A) from their header files. So what you would do is instead place a forward (@class A) declaration in one of the classes' (class B's) header file. However, when attempting to use an ivar of class A within the implementation of class B, this very error comes up, merely adding an #import "A.h"in the .m file of class B fixed the problem for me.

当我有两个相互依赖的文件时,我收到了这种消息。这里的棘手之处在于,如果您只是尝试从它们的头文件中相互导入(A 类导入 B 类,B 类导入 A 类),您将获得循环引用。因此,您要做的是@class A在类的(B 类)头文件之一中放置一个 forward ( ) 声明。但是,当尝试在 B 类的实现中使用 A 类的 ivar 时,就会出现这个错误,只需#import "A.h"在 B 类的 .m 文件中添加一个即可为我解决问题。

回答by Suraj K Thomas

For me i was using @class "Myclass.h"

When i changed to #import"Myclass.h"

it worked fine .

对我来说,我正在使用 @class "Myclass.h"

当我更改为 #import"Myclass.h"

它工作正常。

回答by Arslan

You are using

您正在使用

States states;

where as you should use

你应该在哪里使用

States *states;

Your init method should be like this

你的 init 方法应该是这样的

-(id)init {
  if( (self = [super init]) ) {
      pickedGlasses = 0;
  }
  return self;
}

Now finally when you are going to create an object for States class you should do it like this.

现在终于当你要为 States 类创建一个对象时,你应该这样做。

State *states = [[States alloc] init];

I am not saying this is the best way of doing this. But it may help you understand the very basic use of initializing objects.

我并不是说这是最好的方法。但它可以帮助您理解初始化对象的非常基本的用法。

回答by Kiril S.

If you are getting this error while trying to use Swift class or method in Objective C: you forgot one of 2 steps Apple defined on this diagram:

如果您在 Objective C 中尝试使用 Swift 类或方法时遇到此错误:您忘记了 Apple 在此图中定义的两个步骤之一:

enter image description here

在此处输入图片说明

Example:

例子:

Error shows up in your Test.mfile:

错误显示在您的Test.m文件中:

Receiver 'MyClass' for class message is a forward declaration

类消息的接收器“MyClass”是前向声明

Step 1: check that Test.hhas

第 1 步:检查是否Test.h

@class MyClass;

Step 2: find *-Swift.hfile name in Build Settings(look for Objective-C Generated Interface Header Name). Name will be something like MyModule-Swift.h

第 2 步:*-Swift.hBuild Settings 中找到文件名(寻找Objective-C Generated Interface Header Name)。名称将类似于MyModule-Swift.h

Step 3: check that Test.mimports the above header

第 3 步:检查是否Test.m导入了上述标题

#import "MyModule-Swift.h"

回答by nemesis

Check if you imported the header files of classes that are throwing this error.

检查您是否导入了引发此错误的类的头文件。

回答by Fletch

Make sure the prototype for your unit method is in the .h file.

确保单元方法的原型在 .h 文件中。

Because you're calling the method higher in the file than you're defining it, you get this message. Alternatively, you could rearrange your methods, so that callers are lower in the file than the methods they call.

因为您在文件中调用的方法高于定义它的位置,所以您会收到此消息。或者,您可以重新排列您的方法,以便调用者在文件中低于他们调用的方法。

回答by whyoz

There are two related error messages that may tell you something is wrong with declarations and/or imports.

有两个相关的错误消息可能会告诉您声明和/或导入有问题。

The first is the one you are referring to, which can be generated by NOT putting an #import in your .m (or .pch file) while declaring an @class in your .h.

第一个是您所指的那个,它可以通过在 .h 中声明 @class 时不在 .m(或 .pch 文件)中放入 #import 来生成。

The second you might see, if you had a method in your States class like:

第二个你可能会看到,如果你的 States 类中有一个方法,比如:

- (void)logout:(NSTimer *)timer

after adding the #import is this:

添加#import后是这样的:

No visible @interface for "States" declares the selector 'logout:'

“States”没有可见的@interface 声明选择器“注销:”

If you see this, you need to check and see if you declared your "logout" method (in this instance) in the .h file of the class you're importing or forwarding.

如果你看到这个,你需要检查你是否在你正在导入或转发的类的 .h 文件中声明了你的“注销”方法(在这个例子中)。

So in your case, you would need a:

所以在你的情况下,你需要一个:

- (void)logout:(NSTimer *)timer;

in your States class's .h to make one or both of these related errors disappear.

在您的 States 类的 .h 中,使这些相关错误中的一个或两个消失。