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

后端 未结 9 1000
心在旅途
心在旅途 2021-01-30 01:19

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

states = [states init];

here is init

相关标签:
9条回答
  • 2021-01-30 01:41

    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.
    0 讨论(0)
  • 2021-01-30 01:41

    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:

    Example:

    Error shows up in your Test.m file:

    Receiver 'MyClass' for class message is a forward declaration

    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-Swift.h"
    
    0 讨论(0)
  • 2021-01-30 01:45

    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
    
    0 讨论(0)
  • 2021-01-30 01:45

    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 讨论(0)
  • 2021-01-30 01:45

    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 讨论(0)
  • 2021-01-30 01:47

    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.

    0 讨论(0)
提交回复
热议问题