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

发布于 2024-12-26 05:42:37 字数 453 浏览 4 评论 0原文

在我的 iOS5 应用程序中,我有 NSObject States 类,并尝试初始化它:

states = [states init];

这是 States 中的 init 方法>:

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

    return self;
}

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

接收者类型“States”例如消息是前向声明

是什么意思?我做错了什么?

In my iOS5 app, I have NSObject States class, and trying to init it:

states = [states init];

here is init method in States:

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

    return self;
}

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

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

What does it mean? What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

扮仙女 2025-01-02 05:42:37

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

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

  • 您正在初始化一个对象,而没有对其进行+alloc操作。这不起作用
  • 你将一个对象声明为非指针类型,这也不起作用
  • 你没有在 -init 中调用 [super init]
  • 您已在标头中使用 @class 声明了该类,但从未导入该类。

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

However, there is a lot of 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 @class in the header, but never imported the class.
伴随着你 2025-01-02 05:42:37

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

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:

#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
浅笑轻吟梦一曲 2025-01-02 05:42:37

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

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.

一世旳自豪 2025-01-02 05:42:37

我试图使用@class "Myclass.h"

当我将其更改为 #import "Myclass.h" 时,它工作得很好。

I was trying to use @class "Myclass.h".

When I changed it to #import "Myclass.h", it worked fine.

羁客 2025-01-02 05:42:37

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

在此处输入图像描述

示例:

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

班级消息的接收者“MyClass”是前向声明

在 Obj-C 文件中:

步骤 1:检查 Test.h 是否有

@class MyClass;

步骤 2:找到 *-Swift.h 文件构建设置中的名称(查找Objective-C 生成的接口标头名称)。名称类似于 MyModule-Swift.h

步骤 3:检查 Test.m 是否导入上述标头

#import <MyModule/MyModule-Swift.h>

在 Swift 文件中:

  • 确保 MyClass (或者它的基类)继承 NSObject 类。
  • 确保 @objc 位于您想要从 Obj-C 调用的每个方法之前。
  • 另外,检查目标成员资格部分(在文件检查器中)。

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:

enter image description here

Example:

Error shows up in your Test.m file:

Receiver 'MyClass' for class message is a forward declaration

In Obj-C files:

Step 1: check that Test.h has

@class MyClass;

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

Step 3: check that Test.m imports the above header

#import <MyModule/MyModule-Swift.h>

In Swift file:

  • Ensure MyClass (or it's base class) inherits NSObject class.
  • Ensure @objc is before each method you want call from Obj-C.
  • Also, check Target Membership section (in File Inspector).
書生途 2025-01-02 05:42:37

你正在使用

States states;

你应该使用的地方

States *states;

你的init方法应该像这样

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

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

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

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

You are using

States states;

where as you should use

States *states;

Your init method should be like this

-(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.

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.

冷了相思 2025-01-02 05:42:37

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

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

意犹 2025-01-02 05:42:37

确保您的单元方法的原型位于 .h 文件中。

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

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

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.

你穿错了嫁妆 2025-01-02 05:42:37

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

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

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

- (void)logout:(NSTimer *)timer

添加 #import 后是这样的:

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

如果您看到此内容,则需要检查并查看是否在您所在类的 .h 文件中声明了“logout”方法(在本例中)导入或转发。

因此,在您的情况下,您需要

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

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

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.

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

- (void)logout:(NSTimer *)timer

after adding the #import is this:

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文