PDA

View Full Version : Objective C custom class problems.




cromestant
Mar 11, 2009, 03:54 PM
Hello, i'm trying to get some basic custom class to work on objective-c but can t even get it to compile, yet i see no problems with it, am hoping for some help from these forums.

Ok first of all the idea:
simple app, 1 button on NIB, 1 AppControler with 1 (IBAction) method that is called when the button is pressed.

apart from that i have one custom class called test ( code comming after)

all this class does is have one method for printing some things to NSLog ( experimenting still with this).

Basically, what I want is to pass the AppController instance to the test class instance , so that from the test class ( threaded later on) call a method on the original object ( like for updating a gui).

THe idea is for me to get this working so that i can finish my twitter program. I made a simple twitter client, but when requests took too long, the gui freezed, i packaged the twittter "api" into a class, and made it work with the class ([twitter sendUpdate:@"works!"]) but I still had the "locking" of the gui.

So i want to make all the calls to twitter happen OFF main thread, i ve basically got it working for the update, but it did not work for the others that actually have a response that matters ( the response needs to be passed back to main thread so that I can update the gui) ,so the idea was to instantiate the twitter client passing the object a back-reference to the instantiator object..

I know i m not beeing to clear here, hope you can understand me anyway.

here is the sample code i m doing that does not compile :
AppController.h

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

@interface AppController : NSObject {
test *t;
}
-(IBAction) startVerga:(id)sender;
-(void)diecrap:(id)sender;
@end


AppController.m


#import "AppController.h"
@implementation AppController
-(void)awakeFromNib{
t = [[test alloc] initWithAppcontroller:self];
}
-(IBAction) startVerga:(id)sender
{
NSLog(@"Clicked on button");
}
-(void)diecrap:(id)sender
{
NSLog(@"llegue a crap!");
}
@end


test.h



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

@interface test : NSObject {
AppController *cont;
}
-(id)initWithAppcontroller:(AppController *)con;
@end


test.m
//

#import "test.h"


@implementation test
-(id) initWithAppcontroller:(AppController *)con
{
self=[super init];
cont = con;
return self;
}
@end



I can t seem to find the "proper" way to do this, or have not been able to understand it...
any help on how this is usually done ( worker threads gathering data for the gui, these threads could instantiate objects etc...).

simple sample code is hard to find in this world of cocoa....



kainjow
Mar 12, 2009, 05:45 AM
That approach seems fine to me. What error are you getting?

Instead of putting the #import xxx in the header file, put it in the implementation (.m) file and in the .h file replace it with "@class xxx;" where xxx is the name of the class.

For example this:
#import <Cocoa/Cocoa.h>
#import "test.h"

@interface AppController : NSObject
...
would become this:
#import <Cocoa/Cocoa.h>
@class test;

@interface AppController : NSObject
...
and the #import "test.h" would go in the .m file instead. This might be why you're getting an error.

cromestant
Mar 12, 2009, 09:23 AM
thanks, that did the trick, it now compiles...!!!

I did not know bout that
@class classname

what is that called?

autorelease
Mar 12, 2009, 04:04 PM
thanks, that did the trick, it now compiles...!!!

I did not know bout that
@class classname

what is that called?

It's called forward declaration.