3

When I run this :

@interface Database : NSObject {
      sqlite3 *database;
}

+(void)openDatabase;

@end



@implementation Database

+(void)openDatabase
{
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *databaseNameandPath = [NSString stringWithFormat:@"%@/DatabaseSnax.sqlite",docDir];
    NSString *databasePath=[[NSString alloc]initWithFormat:databaseNameandPath];

    if(sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
    {
        NSLog(@"Error when open the database");
    }

    [databasePath release];
}

I have this error: instance variable database accessed in class method

How I can solve this issue and I need to keep my method (open database) as a static method so I can use it by class name, for example:

[Database openDatabase];
favo
  • 5,426
  • 9
  • 42
  • 61
Muhannad Dasoqie
  • 313
  • 3
  • 17
  • 1
    Please use tags to indicate which language this is (Objective-C?) – John Saunders Jan 15 '12 at 17:12
  • possible duplicate of [Instance variable 'variable' accessed in class method error](http://stackoverflow.com/questions/8016904/instance-variable-variable-accessed-in-class-method-error) – Caleb Jan 15 '12 at 17:49
  • Thanks for your advice, I will indicate the programming language in the next times :) Yes it's Objective-c – Muhannad Dasoqie Jan 23 '12 at 11:07

3 Answers3

3

One cannot possibly access instance variables from class methods. You may declare a global variable, however:

static sqlite3 *database;
// ...
+ (void) openDatabase {
    sqlite3_open(filename, &database);
    // ...
}
  • 1
    Hi H2CO3, Thank you a lot, it is working now .. I know that I can not access any instance variable from class methods, but what I want is how to do this, and your answer helped me ... thanks again :) – Muhannad Dasoqie Jan 16 '12 at 08:27
2

You're attempting to access database from a class method (which are different from instance methods).

Change that declaration from:

+ (void) openDatabase;

to

- (void) openDatabase;

and instantiate your Database object via the traditional alloc + init and you'll be on your way.

I like H2CO3's answer too (and +1 to him), but my answer (which is what most people have to do with Objective C objects) may be more practical for what you are trying to do.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Of course, and it's more elegant, and we don't like global variables. I just decided to write this as he wrote he needed to keep the method static. –  Jan 15 '12 at 17:23
  • Good point (that he wanted to keep the method static). We don't know what he's up to, but in general, I can't imagine why he'd want to do this statically... unless maybe he's mixing a lot of C++ and Objective C (yucky yuck). – Michael Dautermann Jan 15 '12 at 17:24
  • Thanks Michael Dautermann, you and H2CO3 are right, but I need this because I used the sqlite database in many places, and insert, select and delete from the database, therefore I expected that this way is the best way, if you have any suggestions to enhance my code I will be appreciate you ... Thanks in advance – Muhannad Dasoqie Jan 16 '12 at 08:38
2

For reference, static means different things to different people/languages. Objective-C being mostly C plus a bunch of syntax enhancements, the keyword static in Objective-C has the same meaning as it does in C, which relates to the visibility of the symbol with respect to linking. This is subtly-but-importantly different from how Java and C# use the word static.

Objective-C doesn't have syntax for declaring "static" (in Java/C# parlance) or "class" variables. The runtime has "support" for them (witness the existence of: class_getClassVariable) but there's no syntax to declare them, so it's sort of a dead end. (If I had to guess, I'd bet that this feature exists in the runtime to support bridges to other languages/runtimes that use static/class variables.) As other people have suggested, the common way around this is to use global variables (or function statics (static in the C linkage sense.))

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • 1
    +1 for addressing some of the misconceptions embodied in the question. – Caleb Jan 15 '12 at 17:41
  • However, "static" in general means a class method in OO languages. And one can distinguish between class methods and vars/functions declared with the "static" keyword. It seems that the author of this question actually understands it. –  Jan 15 '12 at 17:46
  • @H2CO3 The asker is clearly using the keyword `static` in the Java/C#/"class variable" sense. Your answer uses the keyword `static` in a C context (and the use of the C `static` keyword in your answer is orthogonal to the fact that it's a global variable -- it would still be a global variable without the `static`). That's why I bothered to mention it. – ipmcc Jan 15 '12 at 21:33
  • I know it would be a global variable nevertheless, it's just for making it "internal". –  Jan 15 '12 at 22:08
  • Sounds like everyone's on the same page. :) – ipmcc Jan 15 '12 at 22:08