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

IphoneIosObjective CForward Declaration

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

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

Solution 3 - Iphone

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.

Solution 4 - Iphone

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

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

Solution 5 - Iphone

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

Solution 6 - Iphone

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.

Solution 7 - Iphone

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

Solution 8 - Iphone

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.

Solution 9 - Iphone

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSentineLView Question on Stackoverflow
Solution 1 - IphoneCatfish_ManView Answer on Stackoverflow
Solution 2 - IphonecapikawView Answer on Stackoverflow
Solution 3 - IphoneStunnerView Answer on Stackoverflow
Solution 4 - IphoneSuraj K ThomasView Answer on Stackoverflow
Solution 5 - IphoneohglstrView Answer on Stackoverflow
Solution 6 - IphoneArslanView Answer on Stackoverflow
Solution 7 - IphonenemesisView Answer on Stackoverflow
Solution 8 - IphoneFletchView Answer on Stackoverflow
Solution 9 - IphonewhyozView Answer on Stackoverflow