views:

102

answers:

3

Hi,

I have code snippet,

which is like this...

#import <sqlite3.h>

@interface DatabaseClass : NSObject {

    sqlite3    *database;
}
@property ( nonatomic, retain ) database;//////this//////

@end

In the implementation file;

#import "DatabaseClass.h"

@implementation DatabaseClass

@synthesize database;//////this///////

@end

My question is can I do this(commented things). And If not what is the work arround?

+2  A: 

empirical testing shows that

@property (nonatomic, retain) database;

does not work (specifically, does not compile). however,

@property (nonatomic, assign) sqlite3 *database;

does.

KevinDTimm
The reason that (nonatomic, assign) works, and (nonatomic, retain) doesn't is that the database ivar is not an Objective-C object, it is a C struct, and cannot be retained.
Shawn Craver
man do I hate getting marked down twice for the right answer.
KevinDTimm
+3  A: 

There are two problems with the code.

First, the declaration is incorrect;

@property ( nonatomic, retain ) database;

Compiling this generates an error message something like:

error: expected specifier-qualifier-list before 'database'

The code is missing the type specifier. In particular, it should look like:

@property ( nonatomic, retain ) sqlite3 *database;

Now, with that fixed, you'll see the error as kevindtimm alluded too:

error: property 'database' with 'retain' attribute must be of object type

Change the @property to assign instead of retain.

Or, better yet, use Core Data. Unless you have a SQLite database from some other source for which the schema is set, going to SQLite directly is a waste of engineering time.

bbum
+1  A: 

You can't do that because sqlite3 isn't an Objective C object. Retained properties have to be objective C objects. One easy way you can tell something is an Objective C object is that the type name is usually in CamelCase.

The sqlite3 database handle is just a pointer to a normal C structure. You can make it an assigned property, but that means the connection won't be automatically closed if you assign another value to the property. To make it assigned, property, you would do:

@property ( nonatomic, assign ) database;

You also want to consider if you really want to do things these way. Here are two suggesions:

  1. Consider using Core Data. It takes a bit of effort to learn, but no more than learning sqlite3, and provides a lot of features sqlite doesn't.
  2. You might want to consider why you're exposing the sqlite3 connection as a property. The database manager class should probably have the sqlite3 connection as a private member variable. Create methods for DatabaseClass to open the sqlite3 database, retrieve data, add data, close the database, etc. The rest of your program would call these methods, but wouldn't know that DatabaseClass uses sqlite3 in the background. That way, if you ever decide to change how your data is stored, you would just need to make changes in one place instead of changing all of the classes that interact with DatabaseClass. (One of the ways Core Data can store its data is as an sqlite3 database, so you'd basically be reinventing a much simpler version of Core Data.)
Jacques