Mark Needham

Thoughts on Software Development

Objective C: Expected ‘(‘ before ‘Project’

with one comment

A mistake I’ve made more than a few times while declaring headers in Objective C is forgetting to explicitly import the classes used in the interface definition.

I’ve been refactoring some of the code I wrote earlier in the week and wanted to create a ‘LabelFactory’. I had the following code:

LabelFactory.h

#import <UIKit/UIKit.h>
 
@interface LabelFactory : NSObject {
}
 
+ (UILabel*)createLabelFrom:(Project *)project withXCoordinate:(NSInteger)x withYCoordinate:(NSInteger)y;
 
@end

Which gives this error on compilation:

/Users/mneedham/SandBox/iPad/CIMon/LabelFactory.h:9:0 /Users/mneedham/SandBox/iPad/CIMon/LabelFactory.h:9: error: expected ')' before 'Project'

I’ve been wondering what that error message actually means for a while and more by accident than design I re-read the section of Apple’s documentation on ‘referring to other classes

An interface file declares a class and, by importing its superclass, implicitly contains declarations for all inherited classes, from NSObject on down through its superclass. If the interface mentions classes not in this hierarchy, it must import them explicitly or declare them with the @class directive:

Declaring ‘Project’ with the ‘@class’ directive just above ‘@interface’ helps fix that problem:

...
@class Project;
 
@interface LabelFactory : NSObject {
 
}
...

The original error message I was getting is still slightly mystifying to me…

Be Sociable, Share!

Written by Mark Needham

August 14th, 2010 at 10:33 am

Posted in Objective C

Tagged with

  • This was exactly the problem I was having a while back.
    Thanks for putting up a post about it.