Objective C custom class problems.

Discussion in 'Mac Programming' started by cromestant, Mar 11, 2009.

  1. cromestant
    Expand Collapse
    macrumors member

    Joined:
    Apr 27, 2006
    #1
    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:mad:"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
    Code:
    #import <Cocoa/Cocoa.h>
    #import "test.h"
    
    @interface AppController : NSObject {
    	test *t;
    }
    -(IBAction) startVerga:(id)sender;
    -(void)diecrap:(id)sender;
    @end
    
    
    AppController.m
    Code:
    
    #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
    Code:
    
    
    #import <Cocoa/Cocoa.h>
    #import "AppController.h"
    
    @interface test : NSObject {
    	AppController *cont;
    }
    -(id)initWithAppcontroller:(AppController *)con;
    @end
    
    
    test.m
    Code:
    //
    
    #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....
     
  2. kainjow
    Expand Collapse
    Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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:
    Code:
    #import <Cocoa/Cocoa.h>
    [color=red]#import "test.h"[/color]
    
    @interface AppController : NSObject
    ...
    would become this:
    Code:
    #import <Cocoa/Cocoa.h>
    [color=red]@class test;[/color]
    
    @interface AppController : NSObject
    ...
    and the #import "test.h" would go in the .m file instead. This might be why you're getting an error.
     
  3. cromestant
    Expand Collapse
    thread starter macrumors member

    Joined:
    Apr 27, 2006
    #3
    thanks, that did the trick, it now compiles...!!!

    I did not know bout that
    Code:
    @class classname
    what is that called?
     
  4. autorelease
    Expand Collapse
    macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #4
    It's called forward declaration.
     

Share This Page