PDA

View Full Version : cross referencing classes




phjo
Mar 16, 2008, 08:40 AM
Hi,

Say I have two classes, one is a controller, named AppController, derived from NSObject, and the other is a NSOperation, let's say BigThread, performing a cpu intensive task that won't stop by itself, but that has a stop: method that makes it finish properly.

The cpu intensive task from time to time involves changing some data and refreshing the display accordingly. The controller is in charge of the display.

The controller initiate the cpu intensive thread, keeping the id of the BigThread instance so that it can send stop: message when necessary.

The id of the controller is sent with the initialisation of the BigThread instance, so that the cpu intensive task can keep it and easily provide the controller with changes and new data...

The compiler won't allow this :

Appcontroller.h :

#import <Cocoa/Cocoa.h>
#import "BigThread.h"

@interface AppController : NSObject {
BigThread *process;
}



BigThread.h :

#import <Cocoa/Cocoa.h>
#import "AppController.h"

@interface BigThread : NSOperation {
AppController *parent;
}


as it will refuse to build failing with a syntax error at the AppController *parent; line.

A fix is to replace AppController *parent; with, for example,
NSObject *parent; or even id parent;

but as you can guess, quite a few warnings occur then, the compiler not being sure the methods used apply to the object in question... (The executable works fine though...)

So what is the best practice here ?

- define a protocol to prevent the warnings ? (not sure how to do it right now, but I guess it might be possible)
- completely change the way the cpu intensive task communicate with the controller ? (I'd rather keep it simple there though...)

phjo



hhas
Mar 16, 2008, 11:32 AM
To forward declare an ObjC class:

@class ClassName;

HTH

phjo
Mar 16, 2008, 12:14 PM
Yes, thanks, I thought about that too (after I posted my message though, while reading online the objective-c documentation : http://objc.toodarkpark.net/ ) and almost answered myself except...

It (the code) looks nicer, but turns out to be quite the same as the trick I used : I need access to methods for both the classes that are defined in the header files, so I get lots of warnings at compile time by just declaring the class I want to use...

No problem at run time of course...

My guess right now is that, to get rid of these warnings, either I need to define a protocol for the class I instantiate without the header file, or use distributed objects for the child (cpu intensive task) to communicate with the controller... (I know nothing so far about distributed objects though, and again, I'd rather keep it simple !)

phjo

Catfish_Man
Mar 16, 2008, 01:26 PM
Use @class in the .h, and then include the relevant .h in the .m

phjo
Mar 16, 2008, 01:43 PM
Use @class in the .h, and then include the relevant .h in the .m

That does the trick indeed !

No more warnings at compile time now, thank you !

phjo