2011-03-10 145 views
0

我不明白“为什么”SQLiteDB可能不会响应“-checkIfDatabaseExists”。这是什么原因造成的?我该如何解决? (我真的很接近这个工作,但因为我是一个新手,我仍然有问题)。这是什么原因造成的?

我很感激任何帮助,我可以得到这一点。下面是代码:

#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 

Here is the results of the build:

+2

请将代码粘贴为文本,而不仅仅是截图。 – mkb 2011-03-10 16:37:45

+0

当然......为什么还要写另一个数据库封装(如果这是你在做什么)?核心数据是在iOS/Mac OS X上创建数据库的最佳支持和最佳性能(无需巨大的工程努力)。如果您需要打包任意模式的SQLite数据库,FMDB似乎是一条可行的路。 – bbum 2011-03-10 17:04:37

回答

0

我不是100%地肯定你已经在这里实现什么,但是从我所知道的,你的头定义+ checkIfDatabaseExists作为一个类的方法(尽管您尚未实现它,因此“未完成实施”和“未找到方法定义”警告)。但是,在您的sharedSQLiteDB方法中,您正在对SQLiteDB实例调用-checkIfDatabaseExists,该实例引用了您尚未定义的实例方法。

编辑:好的,看到完整的代码后,我的上述答案显然不是这样。但是你的标题是什么样的?你有+而不是 - 在checkIfDatabaseExists签名前面吗?

0

当你有:编译-foo的时候,因此,将警告编译-fooself可能不-bar回应

- (void) foo 
{ 
    [self bar]; 
} 

- (void) bar 
{ ... } 

编译器已不见-bar定义。

如果该方法的目的是完全私人的类,然后做这在.m文件的顶部:

@interface Foo() 
- (void) bar; 
@end 

如果它的目的是通过其他类调用,添加方法声明到@interface

相关问题