“Expected a type” error pointing to the return type of a method

前端 未结 8 1545
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 06:02

I\'ve attempted to compile, but every time I do, one method throws a strange \"expected a type\" error. I have a method in the header:

-(ANObject *)generateS         


        
相关标签:
8条回答
  • 2020-12-13 06:32

    This is to do with the order that the source files are compiled in. You are already probably aware that you can't call a method before it is defined (see below pseudocode):

    var value = someMethod();
    
    function someMethod()
    {
        ...
    }
    

    This would cause a compile-time error because someMethod() has not yet been defined. The same is true of classes. Classes are compiled one after the other by the compiler.

    So, if you imagine all the classes being put into a giant file before compilation, you might be able to already see the issue. Let's look at the Ship and BoatYard class:

    @interface BoatYard : NSObject
    @property (nonatomic, retain) Ship* currentShip;
    @end
    
    @interface Ship : NSObject
    @property (nonatomic, retain) NSString* name;
    @property (nonatomic, assign) float weight;
    @end
    

    Once again, because the Ship class has not yet been defined, we can't refer to it yet. Solving this particular problem is pretty simple; change the compilation order and compile. I'm sure you're familliar with this screen in XCode:

    But are you aware that you can drag the files up and down in the list? This changes the order that the files will be compiled in. Therefore, just move the Ship class above the BoatYard class, and all is good.

    But, what if you don't want to do that, or more importantly, what if there is a circular relationship between the two objects? Let's increase the complexity of that object diagram by adding a reference to the current BoatYard that the Ship is in:

    @interface BoatYard : NSObject
    @property (nonatomic, retain) Ship* currentShip;
    @end
    
    @interface Ship : NSObject
    @property (nonatomic, retain) BoatYard* currentBoatYard;
    @property (nonatomic, retain) NSString* name;
    @property (nonatomic, assign) float weight;
    @end
    

    Oh dear, now we have a problem. These two can't be compiled side-by-side. We need a way to inform the compiler that the Ship* class really does exist. And this is why the @class keyword is so handy.

    To put it in layman's terms, you're saying, "Trust me man, Ship really does exist, and you'll see it really soon". To put it all together:

    @class Ship;
    
    @interface BoatYard : NSObject
    @property (nonatomic, retain) Ship* currentShip;
    @end
    
    @interface Ship : NSObject
    @property (nonatomic, retain) BoatYard* currentBoatYard;
    @property (nonatomic, retain) NSString* name;
    @property (nonatomic, assign) float weight;
    @end
    

    Now the compiler knows as it compiles BoatYard, that a Ship class definition will soon appear. Of course, if it doesn't, the compilation will still succeed.

    All the @class keyword does however is inform the compiler that the class will soon come along. It is not a replacement for #import. You still must import the header file, or you will not have access to any of the class internals:

    @class Ship
    
    -(void) example
    {
        Ship* newShip = [[Ship alloc] init];
    }
    

    This cannot work, and will fail with an error message saying that Ship is a forward declaration. Once you #import "Ship.h", then you will be able to create the instance of the object.

    0 讨论(0)
  • 2020-12-13 06:32

    So, for some reason I was getting this error while trying to set a method with an enum type in the parameters. Like so:

    - (void)foo:(MyEnumVariable)enumVariable;
    

    I had previously used it like this and never had an issue but now I did. I checked for circular dependency and could find none. I also checked for typos multiple times and no dice. What ended up solving my issue was to adding 'enum' before I wanted to access the variable. Like so:

    - (void)foo:(enum MyEnumVariable)enumVariable;
    {
         enum MyEnumVariable anotherEnumVariable;
    }
    
    0 讨论(0)
  • 2020-12-13 06:35

    You basically add

    @class ANObject;
    

    before @interface!

    0 讨论(0)
  • 2020-12-13 06:37

    It may sound stupid, but wrong shelling or wrong use of uppercase/lowercase letterwrong case this.

    0 讨论(0)
  • 2020-12-13 06:41

    I found this error hapenning when there is circular dependency on the headers. Check if the .h file where you declare this method is imported in ANObject.h

    0 讨论(0)
  • 2020-12-13 06:43

    Strangely enough, changing the order of my imports has fixed this in the past... Try moving the import to the bottom after all your other imports.

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