Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 16, 2008, 08:40 AM   #1
phjo
macrumors regular
 
Join Date: Jan 2008
cross referencing classes

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 :
Code:
#import <Cocoa/Cocoa.h>
#import "BigThread.h"

@interface AppController : NSObject {
	BigThread *process;
}
BigThread.h :
Code:
#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
phjo is offline   0 Reply With Quote
Old Mar 16, 2008, 11:32 AM   #2
hhas
macrumors regular
 
Join Date: Oct 2007
To forward declare an ObjC class:

Code:
@class ClassName;
HTH
__________________
Learn AppleScript, 3rd edition, Sanderson & Rosenthal:
http://www.apress.com/9781430223610
hhas is offline   0 Reply With Quote
Old Mar 16, 2008, 12:14 PM   #3
phjo
Thread Starter
macrumors regular
 
Join Date: Jan 2008
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
phjo is offline   0 Reply With Quote
Old Mar 16, 2008, 01:26 PM   #4
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Use @class in the .h, and then include the relevant .h in the .m
Catfish_Man is offline   0 Reply With Quote
Old Mar 16, 2008, 01:43 PM   #5
phjo
Thread Starter
macrumors regular
 
Join Date: Jan 2008
Quote:
Originally Posted by Catfish_Man View Post
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
phjo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
All iPads: Using iPad for online classes rpearlberg iPad 2 Nov 23, 2013 09:59 AM
Referencing a child class in a parent class, good or bad? SAIRUS iPhone/iPad Programming 11 Aug 28, 2013 11:59 AM
XCode 4.2 and referencing external files BlinksTale Mac Programming 7 Nov 30, 2012 02:41 PM
Referencing Controls within / from sub views CylonGlitch Mac Programming 14 Jun 25, 2012 08:06 PM
Functions, classes and ivars jivaro Mac Programming 6 Jun 10, 2012 03:26 PM

Forum Jump

All times are GMT -5. The time now is 12:00 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC