I don't understand "why" SQLiteDB may not respond to "-checkIfDatabaseExists". What's causing this and how do I fix it? (I'm real close to getting this to work, but because I'm a newbie, I am still having problems).
I appreciate any assistance I can get on this. Here is the code:
#import "SQLiteDB.h"
static SQLiteDB *sharedSQLiteDB = nil; // makes this a singleton class
@implementation SQLiteDB
@synthesize db, dbPath, databaseKey;
//-------------- check for database or create it ----------------|
#pragma mark Singleton Methods
+ (SQLiteDB *) sharedSQLiteDB {
if(!sharedSQLiteDB) {
sharedSQLiteDB = [[SQLiteDB alloc] init];
[sharedSQLiteDB checkIfDatabaseExists开发者_运维百科];
}
return sharedSQLiteDB;
}
+(id)allocWithZone:(NSZone *)zone {
if(!sharedSQLiteDB) {
sharedSQLiteDB = [super allocWithZone:zone];
return sharedSQLiteDB;
}
else {
return nil;
}
}
-(id)copyWithZone:(NSZone *)zone {
return self;
}
-(void) release {
// no-op
}
- (void) checkIfDatabaseExists {
// Get the path to the database file
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
NSString *databasePath = [documentPath stringByAppendingPathComponent:@"ppcipher.s3db"];
// Open the database file
const char *cDatabasePath = [databasePath cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_open(cDatabasePath, &db) == SQLITE_OK) // does it exist?
return;
else { // create database file here
}
}
@end
I'm not 100% sure about what you've implemented here, but from what I can tell, your header has defined +checkIfDatabaseExists as a class method (although you haven't implemented it yet, hence the "incomplete implementation" and "method definition not found" warnings). However, in your sharedSQLiteDB method, you are calling -checkIfDatabaseExists on an instance of SQLiteDB which refers to a instance method that you have not defined.
Edit: Okay, after seeing the full code, my above answer is obviously not the case. But what does your header look like? Do you have a + instead of a - in front of the checkIfDatabaseExists signature?
When you have:
- (void) foo
{
[self bar];
}
- (void) bar
{ ... }
The compiler hasn't seen the definition of -bar
when compiling -foo
and, thus, will warn that self
may not respond to -bar
when compiling -foo
.
If that method is intended to be entirely private to the class, then do this at the top of the .m file:
@interface Foo()
- (void) bar;
@end
If it is intended to be called by other classes, add the method declaration to the @interface
.
精彩评论