在Swift 2中,这仍然是一个问题。如上所述,最好的解决方案是使用桥接标头并在Objective C中捕获NSException。
https://medium.com/swift-programming/adding-try-catch-to-
swift-71ab27bcb5b8描述了一个很好的解决方案,但是确切的代码无法在Swift
2中编译,因为
try并且
catch现在是保留关键字。您需要更改方法签名以解决此问题。这是一个例子:
// https://medium.com/swift-programming/adding-try-catch-to-swift-71ab27bcb5b8@interface TryCatch : NSObject+ (void)tryBlock:(void (^)())try catchBlock:(void (^)(NSException *))catch finallyBlock:(void (^)())finally;@end@implementation TryCatch+ (void)tryBlock:(void (^)())try catchBlock:(void (^)(NSException *))catch finallyBlock:(void (^)())finally { @try { try ? try() : nil; } @catch (NSException *e) { catch ? catch(e) : nil; } @finally { finally ? finally() : nil; }}@end


