如何从 Swift class 中抛出 NSError 并在 Objective-C class 中捕获它?
How can I throw an NSError from a Swift class and catch it in an Objective-C class?
我需要在 Objective-C 中实现一个 try-catch 结构来处理 Swift 抛出的 NSErrors。
我用 Swift 代码编写了一个 NetService 管理器,我正在一个已经存在的 Objective-C UI.
上实现它
但是,每当我从 Swift class 中抛出错误时,try-catch 结构无法捕获错误并继续执行 finally 块。
Swift 错误定义:
enum NEONetServiceErrors: Int
{
case requestMadeWithoutIp
}
struct NEONetServiceErrorStrings
{
let requestMadeWithoutIp = ["NEONetService Error: Request made without IP": NEONetServiceErrors.requestMadeWithoutIp]
}
Swift 抛出错误:
@objc func requestHelloPage() throws
{
if serviceiPAddress != nil
{
makeHelloRequest()
}
else
{
throw NSError(domain: errorStrings.domain, code: NEONetServiceErrors.requestMadeWithoutIp.rawValue, userInfo:errorStrings.requestMadeWithoutIp)
}
}
Objective-C 属性:
@property NEONetServiceManager* netServiceManager;
@property NSError* _Nullable __autoreleasing * _Nullable netServiceError;
Objective-C 错误处理:
- (IBAction)pressUpdateButton:(id)sender
{
@try
{
[self.netServiceManager requestHelloPageAndReturnError: self.netServiceError];
}
@catch (NSException *exception)
{
NSLog(@"Throwing");
}
@finally
{
NSLog(@"Finally");
}
}
输出:
2019-10-18 14:47:03.289268-0300 NEOFirmUpdate[16533:2389800] Start
2019-10-18 14:47:03.292696-0300 NEOFirmUpdate[16533:2389800] Finally
你能帮我弄清楚我的错误处理有什么问题吗?
问题是 Swift 错误/Objective-C NSError 不是 NSException。您已配置为捕获 NSExceptions,但这无关紧要。
当 Swift 抛出错误时 "catch" 在 Objective-C 中出现 NSError 的方法是间接使用 NSError**
参数,就像它一直以来那样。
NSError* err = nil;
BOOL ok = [self.netServiceManager requestHelloPageAndReturnError:&err];
if (ok) {
// proceed normally
} else {
// you got an error, it is sitting in `err`
}
(请注意 Swift 如何准确提供 BOOL 结果,以便您可以实施正确的模式。)
那是因为您在那里使用 objective-c 异常,而不是实际检查错误。要检查 objective-c 中的错误,您传递对指针的引用,如果出现问题,您的函数将填补该错误。
NSError *serviceError = nil;
[self.netServiceManager requestHelloPageAndReturnError:&serviceError];
if (serviceError) {
// there was a problem
}
如果这是一个异步调用,您需要在闭包中执行此操作:
NSError *serviceError = nil;
[self.netServiceManager requestHelloPage:^(NSError *error) {
if (error) {
// there was a problem
}
}];
在您的 Objective-C 代码中,您捕获的是 NSException,而不是 NSError
Swift automatically bridges between the Error type and the NSError
class. Objective-C methods that produce errors are imported as Swift
methods that throw, and Swift methods that throw are imported as
Objective-C methods that produce errors, according to Objective-C
error conventions.
更多信息您可以点击here
我需要在 Objective-C 中实现一个 try-catch 结构来处理 Swift 抛出的 NSErrors。
我用 Swift 代码编写了一个 NetService 管理器,我正在一个已经存在的 Objective-C UI.
上实现它但是,每当我从 Swift class 中抛出错误时,try-catch 结构无法捕获错误并继续执行 finally 块。
Swift 错误定义:
enum NEONetServiceErrors: Int
{
case requestMadeWithoutIp
}
struct NEONetServiceErrorStrings
{
let requestMadeWithoutIp = ["NEONetService Error: Request made without IP": NEONetServiceErrors.requestMadeWithoutIp]
}
Swift 抛出错误:
@objc func requestHelloPage() throws
{
if serviceiPAddress != nil
{
makeHelloRequest()
}
else
{
throw NSError(domain: errorStrings.domain, code: NEONetServiceErrors.requestMadeWithoutIp.rawValue, userInfo:errorStrings.requestMadeWithoutIp)
}
}
Objective-C 属性:
@property NEONetServiceManager* netServiceManager;
@property NSError* _Nullable __autoreleasing * _Nullable netServiceError;
Objective-C 错误处理:
- (IBAction)pressUpdateButton:(id)sender
{
@try
{
[self.netServiceManager requestHelloPageAndReturnError: self.netServiceError];
}
@catch (NSException *exception)
{
NSLog(@"Throwing");
}
@finally
{
NSLog(@"Finally");
}
}
输出:
2019-10-18 14:47:03.289268-0300 NEOFirmUpdate[16533:2389800] Start
2019-10-18 14:47:03.292696-0300 NEOFirmUpdate[16533:2389800] Finally
你能帮我弄清楚我的错误处理有什么问题吗?
问题是 Swift 错误/Objective-C NSError 不是 NSException。您已配置为捕获 NSExceptions,但这无关紧要。
当 Swift 抛出错误时 "catch" 在 Objective-C 中出现 NSError 的方法是间接使用 NSError**
参数,就像它一直以来那样。
NSError* err = nil;
BOOL ok = [self.netServiceManager requestHelloPageAndReturnError:&err];
if (ok) {
// proceed normally
} else {
// you got an error, it is sitting in `err`
}
(请注意 Swift 如何准确提供 BOOL 结果,以便您可以实施正确的模式。)
那是因为您在那里使用 objective-c 异常,而不是实际检查错误。要检查 objective-c 中的错误,您传递对指针的引用,如果出现问题,您的函数将填补该错误。
NSError *serviceError = nil;
[self.netServiceManager requestHelloPageAndReturnError:&serviceError];
if (serviceError) {
// there was a problem
}
如果这是一个异步调用,您需要在闭包中执行此操作:
NSError *serviceError = nil;
[self.netServiceManager requestHelloPage:^(NSError *error) {
if (error) {
// there was a problem
}
}];
在您的 Objective-C 代码中,您捕获的是 NSException,而不是 NSError
Swift automatically bridges between the Error type and the NSError class. Objective-C methods that produce errors are imported as Swift methods that throw, and Swift methods that throw are imported as Objective-C methods that produce errors, according to Objective-C error conventions.
更多信息您可以点击here