我使用以下语句调用以下代码: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