gpt4 book ai didi

ios - 错误 : catch parameter is not a pointer to an interface type iOS

转载 作者:行者123 更新时间:2023-11-29 10:58:50 25 4
gpt4 key购买 nike

我正在尝试在我的 Xcode 项目中使用异常处理。

- (void)viewDidLoad
{
[super viewDidLoad];

authenticate = [[UseDb alloc]init];
@try{
[authenticate createDatabase];
}
@catch (DatabaseCreationException) {
NSLog(@"failed to create database");
}
}

这是抛出异常的方法:

-(void)createDatabase
{

NSString *docsDir;
NSArray *dirPaths;
char *sql_stmt1,*sql_stmt2,*sql_stmt3,*sql_stmt4;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
_mDatabasePathDb = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"SmartDiaryDatabaseTest2.sqlite"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: _mDatabasePathDb ] == NO)
{
const char *dbpath = [_mDatabasePathDb UTF8String];

if (sqlite3_open(dbpath, &_mDb) == SQLITE_OK)
{

char *errMsg;

sql_stmt1 = "CREATE TABLE IF NOT EXISTS USERDETAIL (mUserName TEXT PRIMARY KEY, mPassword TEXT,mUname TEXT, mImage TEXT, mGender CHAR, mDob DATE, mEmail TEXT, mAddress TEXT, mMaritalStatus BOOL, mLatitude DOUBLE, mLongitude DOUBLE)";

if (sqlite3_exec(_mDb, sql_stmt1, NULL, NULL, &errMsg) != SQLITE_OK)
{

}

sql_stmt2 = "CREATE TABLE IF NOT EXISTS CONTACTS (mName TEXT, mContactno TEXT, mCgender CHAR, mCdob DATE, mCemail TEXT, mClatitude DOUBLE, mClongitude DOUBLE)";

if (sqlite3_exec(_mDb, sql_stmt2, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"failed to create table");

}

sql_stmt3 = "CREATE TABLE IF NOT EXISTS TODO (mStartDate DATE, mEndDate DATE, mTaskName TEXT, mTaskDescription TEXT, mPriority INT)";

if (sqlite3_exec(_mDb, sql_stmt3, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"failed to create table");
}


sql_stmt4 = "CREATE TABLE IF NOT EXISTS FAVORITES (mTdate DATE, mNewsLabel TEXT )";

if (sqlite3_exec(_mDb, sql_stmt4, NULL, NULL, &errMsg) != SQLITE_OK)
{

}
// sqlite3_close(_mDb);

} else {
NSLog(@"failed to create database");
NSException *e = [DatabaseCreationException exceptionWithName:@"DatabaseCreationFailedException" reason:@"sqlite execution failed" userInfo:nil];
@throw e;

}

}
sqlite3_close(_mDb);
}

我收到问题中提到的错误:catch parameter is not a pointer to an interface type iOS 我第一次在 Objective C 中处理异常。请告诉我我在哪里我错了。我创建了一个名为 DatabaseCreationException 的新类并将其导入。

最佳答案

你需要通过指针来捕获异常,像这样:

@try{
[authenticate createDatabase];
}
@catch (DatabaseCreationException*ex) {
// ^
NSLog(@"failed to create database");
}

请注意,Apple 强烈建议不要使用异常来处理运行时情况,将它们保留在应用程序即将关闭的情况下(即不可恢复的情况)。如果情况可以恢复,Apple 建议使用 NSError ( Exceptions Programming Guide )。

关于ios - 错误 : catch parameter is not a pointer to an interface type iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16980588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com