我有这段代码,(我没写过):
- (BOOL)execute:(NSString *)sqlCommand error:(NSError **)error
{
const char *sql = [sqlCommand UTF8String];
char *errorPointer;
if (sqlite3_exec(database, sql, NULL, NULL, &errorPointer) != SQLITE_OK)
{
if (error)
{
NSString *errMsg = [NSString stringWithCString:errorPointer encoding:NSUTF8StringEncoding];
NSString *description = @"An error occurred executing the SQL statement";
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:description, NSLocalizedDescriptionKey, errMsg, NSLocalizedFailureReasonErrorKey, nil];
*error = [[[NSError alloc] initWithDomain:SQLCipherManagerErrorDomain code:ERR_SQLCIPHER_COMMAND_FAILED userInfo:userInfo] autorelease];
sqlite3_free(error);
}
return NO;
}
return YES;
}
我也有一个没有“error”参数的方法,我会用这样的方法调用:[dm execute:insertCommand];
我的问题是:我将使用带有“error”参数的方法调用的格式是什么,所以我可以看到来自调用的任何错误?
答案 0 :(得分:3)
NSError* error = nil;
BOOL success = [dm execute:insertCommand error:&error];
if (success == NO)
{
// do whatever if failed
}
另见cocoa文档中的"Handling Error Objects Returned From Methods"。
答案 1 :(得分:2)
NSError *error = nil; //your error
if(![dm execute:sqlCommand error:&error])
{ //Pass the address to your pointer ^^
//Since you passed the address of your error pointer
//your execute command was able to set it to an
//NSError if one occurrs
}
答案 2 :(得分:0)
您必须在代码调用之外声明一个NSError
对象,并传入地址:
NSError *error;
BOOL success = [whateverObj execute:theSqlStmt error:&error];
if (!success) {
// Handle error
}