PDA

View Full Version : Accessing object from a class method




simaomm
Mar 10, 2009, 05:21 PM
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


-(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];
}



lee1210
Mar 10, 2009, 05:45 PM
make an accessor and call it on your instance db in doHistory:withDB?

-Lee

simaomm
Mar 10, 2009, 05:58 PM
make an accessor and call it on your instance db in doHistory:withDB?

-Lee

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:


-(id) initWithDB: (BatMeasureDB*)pdb
{

[super init];

[NSThread detachNewThreadSelector:@selector(doHistory:withDB:) toTarget:[BatWatcher class] withObject:pdb];

return self;
}


And


BatMeasureDB* pdb = [BatMeasureDB alloc] init];


Is executed before calling initWithDB.


I tried using:


[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

kpua
Mar 10, 2009, 06:37 PM
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.

simaomm
Mar 10, 2009, 07:09 PM
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.

Thank you!

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