Ollie Hirst
Ollie Hirst

Reputation: 592

"Expected a type" error pointing to the return type of a method

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 *)generateSomethingForSomethingElse:(NSString *)somethingElse;

The error points at the return type for this method. I've imported ANObject into the header using #import "ANObject.h" and ANObject is compiling fine..

Why is this happening?

Upvotes: 44

Views: 64492

Answers (9)

NSparesh
NSparesh

Reputation: 19

I solved it by adding @class class_name to the .h file

Upvotes: 0

DranoMax
DranoMax

Reputation: 504

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.

Upvotes: 0

sean808080
sean808080

Reputation: 252

I got this message, when the variable type was misspelled. See below this below

e.g.

-(void)takeSimulatorSafePhotoWithPopoverFrame:(GCRect)popoverFrame {

instead of.....

-(void)takeSimulatorSafePhotoWithPopoverFrame:(CGRect)popoverFrame {

Upvotes: 1

JomanJi
JomanJi

Reputation: 1417

You basically add

@class ANObject;

before @interface!

Upvotes: 27

Yeung
Yeung

Reputation: 2231

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

Upvotes: 1

olivaresF
olivaresF

Reputation: 1379

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;
}

Upvotes: 4

Steve Rukuts
Steve Rukuts

Reputation: 9367

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.

Upvotes: 99

RENA
RENA

Reputation: 561

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

Upvotes: 56

Dave DeLong
Dave DeLong

Reputation: 243156

Usually when I see an error like this it's because I have a typo on a previous line, such as an extra or missing parenthesis or something.

Upvotes: 1

Related Questions