Error: Expected specifier-qualifier-list before XXX

前端 未结 3 421
情歌与酒
情歌与酒 2021-01-28 23:50

it did it so many times and i didn\'t had any problems with it, but this time i still getting the error above, my relevant code is this:

#import \"PiecesChoisies         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-29 00:16

    PiecesChoisies is not recognized as a type. This may happen because it has cyclical dependencies.

    The following code example illustrates the problem. Classes A and B create a circular dependency trying to import each other.

    #import "B.h"               // <-- A imports B
    @interface A : NSObject
    @end
    
    #import "A.h"
    @implementation A
    @end
    
    
    #import "A.h"              // <-- and B imports A
    @interface B : NSObject
    @end
    
    #import "B.h"
    @implementation B
    @end
    

    Because the classes are never created, the compiler treats them as unknown tokens, so the error shows as Expected specifier-qualifier-list before XXX. In other words "I expected something meaningful before XXX".

    To remove the circular dependency:

    1. And add a forward class declaration (@class) on the interface file.
    2. Move the #import from the interface to the implementation file.

    The class declaration tells the compiler "don't worry, I'll define this later", so the header becomes safe to import because the conflicting definition is now out of sight on the implementation file.

    Here is the result for the previous example:

    @class B;             // <---- #import "B.h" replaced with @class B
    @interface A : NSObject
    @end
    
    #import "A.h"
    #import "B.h"         // <---- #import "B.h" added
    @implementation A
    @end
    

    And do the same with class B:

    @class A;             // <---- #import "A.h" replaced with @class A
    @interface B : NSObject
    @end
    
    #import "B.h"
    #import "A.h"         // <---- #import "A.h" added
    @implementation B
    @end
    

提交回复
热议问题