Re: Scope in a class
Re: Scope in a class
- Subject: Re: Scope in a class
- From: Jon Hull <email@hidden>
- Date: Mon, 6 Jun 2005 16:18:25 -0700
It looks like a retain/release problem to me. The object is probably
getting released (auto-released in this case) before your other
action is called. Easy to fix.
In Cocoa, we generally use "accessor methods" to avoid such
problems. For every class variable, you have two methods. One to
return the variable, and one to set its value. In this case:
-(QuickLiteDatabase*) d_dbase
{
return d_dbase;
}
-(void) setD_dbase:(QuickLiteDatabase*)newDbase
{
[newDbase retain];
[d_dbase release];
d_dbase=newDbase;
}
Then your code looks like this:
- (IBAction)newDB:(id)sender
{
[self setD_dbase: [QuickLiteDatabase
databaseWithFile:d_dbaseNome]];
}
- (IBAction)dummy:(id)sender
{
[[self d_dbase] performQuery:@"SELECT creatore FROM datiGenerali;"];
}
Also, you may want to consider more readable names like "database"
thanks,
Jon
Hi everybody.
I know Cocoa is very strict 'bout scoping, but I can't solve this.
The problem is I can't access a variable if I have allocated it into
another method.
In this example, from the "dummy" method, the variable d_dbase is out
of scope.
Why, if it's declared as public in the header file?
Thanks to all for the kind answers.
------------------H E A D E R F I L E ------------------
@interface myGTEd : NSObject
{
@public
QuickLiteDatabase* d_dbase;
}
@end
---------------------------------------------------------
------------------C L A S S F I L E ------------------
@implementation myGTEd
- (IBAction)newDB:(id)sender
{
d_dbase= [QuickLiteDatabase databaseWithFile:d_dbaseNome];
}
- (IBAction)dummy:(id)sender
{
[d_dbase performQuery:@"SELECT creatore FROM datiGenerali;"];
}
---------------------------------------------------------
--
Sanri Parov
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden