我使用以下語句調用以下代碼:SQLiteDB * db = [[[SQLiteDB alloc] init] autorelease];Singleton未正確初始化
問題是「sharedSQLiteDB」沒有被調用,而是「allocWithZone」,因此「checkIfDatabaseExists」沒有被調用,這是數據庫創建的地方。
我不明白爲什麼...(即我究竟做錯了什麼?)
#import "SQLiteDB.h"
static SQLiteDB *sharedSQLiteDB = nil; // makes this a singleton class
@implementation SQLiteDB
@synthesize searchPaths, documentPath, databasePath, cDatabasePath;
#pragma mark Singleton Methods
+ (SQLiteDB *) sharedSQLiteDB {
if(!sharedSQLiteDB) {
sharedSQLiteDB = [[SQLiteDB alloc] init];
[sharedSQLiteDB checkIfDatabaseExists]; // check to see if d/b exists
}
return sharedSQLiteDB;
}
+(id)allocWithZone:(NSZone *)zone { // makes sure another instance is not allocated
if(!sharedSQLiteDB) {
sharedSQLiteDB = [super allocWithZone:zone];
return sharedSQLiteDB;
}
else {
return nil;
}
}
-(id)copyWithZone:(NSZone *)zone {
return self;
}
-(void) release {
// no-op
}
非常感謝......現在正在工作......我感謝大家的輸入...... – SpokaneDude 2011-04-29 17:41:19