Accessing object from a class method

Discussion in 'Mac Programming' started by simaomm, Mar 10, 2009.

  1. macrumors newbie

    Joined:
    Dec 16, 2008
    #1
    Hello,

    I am trying to access some memory, belonging to an instance, from a class method.

    That is, I am trying to access pdb, from doHistory.

    I understand I can't do that, but how can I access pdb from doHistory? Is there any other way?

    Thank you,
    Simão

    Code:
    -(id) initWithDB: (BatMeasureDB*)pdb
    {
    	[super init];
    
    	BatMeasureDB* pdb = [BatMeasureDB alloc] init];
    
    	[NSThread detachNewThreadSelector:@selector(doHistory:withDB:) toTarget:[BatWatcher class] withObject:pdb];
    	
    	return self;
    }
    
    
    +(void) doHistory:(id)param withDB:(BatMeasureDB*)db
    {
    		[db toString];
    }
    
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    make an accessor and call it on your instance db in doHistory:withDB?

    -Lee
     
  3. thread starter macrumors newbie

    Joined:
    Dec 16, 2008
    #3
    The problem is that I want to pass it as an argument when calling initWithDB.

    The actual BatMeasureDB object belongs to another class.

    But you are right, I was trying to simplify so I could explain.

    Actually I have something like:

    Code:
    -(id) initWithDB: (BatMeasureDB*)pdb
    {
    
    	[super init];
    
    	[NSThread detachNewThreadSelector:@selector(doHistory:withDB:) toTarget:[BatWatcher class] withObject:pdb];
    	
    	return self;
    }
    
    And

    Code:
     
    BatMeasureDB* pdb = [BatMeasureDB alloc] init];
    
    Is executed before calling initWithDB.


    I tried using:

    Code:
    	[NSThread detachNewThreadSelector:@selector(doHistory:withDB:) toTarget:self withObject:db];
    
    So doHistory is now an object method and not a class method, but I still can't access the memory region pointed by (BatMeasureDB*)pdb inside doHistory.

    Thank you for your reply,
    SM
     
  4. macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    The selector you pass to detachNewThreadSelector:toTarget:withObject: may only have one argument (see the NSThread docs). That argument will be the "withObject:" object you passed in.

    Your doHistory:withDB: selector has two arguments, and it looks like the first is completely unnecessary since you don't do anything with it. Try renaming it to doHistoryWithDB: and eliminate the first object.
     
  5. thread starter macrumors newbie

    Joined:
    Dec 16, 2008
    #5
    Thank you!

    I didn't realize I left "param" there when I added withDB.
     

Share This Page