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

states = [states init];

这是init方法在各州:

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

    return self;
}

但是在lines states = [states init]中有错误;

接收端类型为“States”的实例消息是前向声明

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


当前回答

检查是否导入了抛出此错误的类的头文件。

其他回答

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

#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

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

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

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

当我把它改为#import“Myclass.h”时,它工作得很好。

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

但是,您的代码中还有许多其他错误。

你在-init一个对象而没有+alloc它。这是行不通的 你把一个对象声明为非指针类型,这也不行 在-init中没有调用[super init]。 您已经在头文件中使用@class声明了该类,但从未导入该类。

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.