219

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?

3
  • I tryed to fide same question, but there is different solution in each of tham. I'm confused a little
    – SentineL
    Jan 11, 2012 at 6:45
  • 4
    The very short answer is you're either missing a #import (perhaps in your Prefix file) or you're missing a "@class Abc;" line (ie in a .h file just above the @interface line)
    – Fattie
    Mar 2, 2014 at 9:17
  • Also make sure if this is a swift class and your using the "@objc(ClassName)" that this name matches your class name definition. Case matters.
    – danroose
    Oct 14, 2020 at 12:40

9 Answers 9

463

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.
4
  • This is a bit weird though since in my case I'd just removed the .h file because of a circular reference issue.
    – Alper
    Nov 16, 2017 at 10:49
  • What an absolutely ridiculous wording. But, yep, that fixed it.
    – TimJowers2
    Aug 21, 2018 at 18:41
  • 3
    If you're trying to use Swift objects in Objective-C, don't forget they have to inherit from NSObject. Feb 20, 2020 at 14:16
  • You could also get this error when placing 'MyProject-Swift.h' within #ifdef FB_SONARKIT_ENABLED or #if DEBUG - then it works in simulator but not when building a release.
    – scbojer
    Mar 4, 2021 at 10:06
28

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
1
  • 2
    What a shame that the almost all CoreData tutorial start with use the template but not with an existing project. It is quite easy to make mistake here.
    – Yeung
    Oct 15, 2013 at 9:04
26

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.

0
18

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

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

1
  • 1
    Just a side-note here: @class "Myclass.h" is completely incorrect. @class Myclass should be used in a header file where the class cannot be imported (due to circular referencing, such as would occur with a Swift-defined class in Objective-C) but #import "Myclass.h" should be used if it can be imported. Jun 19, 2020 at 15:57
16

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).
6

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.

0
4

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

1
  • lol That was easy Jun 1, 2022 at 22:37
1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.