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?
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):
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
ShipandBoatYardclass:Once again, because the
Shipclass 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
Shipclass above theBoatYardclass, 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
BoatYardthat theShipis in: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
@classkeyword is so handy.To put it in layman’s terms, you’re saying, “Trust me man,
Shipreally does exist, and you’ll see it really soon”. To put it all together:Now the compiler knows as it compiles
BoatYard, that aShipclass definition will soon appear. Of course, if it doesn’t, the compilation will still succeed.All the
@classkeyword 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: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.