简体   繁体   中英

Error Handling an Objective-C framework in Swift Language

I'm creating an iOS Swift App, using an Objective-C Framework.

The framework header file (SMPort.h):

// ... do all the import //

@interface PortException : NSException
{
}

@end

@interface PortInfo : NSObject

- (id)initWithPortName:(NSString *)portName_ macAddress:(NSString *)macAddress_ modelName:(NSString *)modelName_;

@property(retain, readonly) NSString *portName;
@property(retain, readonly) NSString *macAddress;
@property(retain, readonly) NSString *modelName;
@property(readonly, getter=isConnected) BOOL connected;

@end

@interface SMPort : NSObject {
    void * m_port;
    WBluetoothPort* wBluetoothPort;
    BluetoothPort* bluetoothPort;
    NSString * m_portName;
    NSString * m_portSettings;
    int m_ioTimeoutMillis;

    BOOL checkedBlockSupport;
}

@property(assign, readwrite, nonatomic) u_int32_t endCheckedBlockTimeoutMillis;

// Initializer and staff methods...

/*!
*  This function retreives the device's detailed status.
*
*  @param starPrinterStatus Pointer to a StarPrinterStatus_n structure where the devices detailed status is written
*                           (either StarPrinterStatus_0, StarPrinterStatus_1, or StarPrinterStatus_2).
*  @param level             Integer designating the level of status structure (either 0, 1, or 2).
*
*  @note Throws PortException on failure.
*/
- (void)getParsedStatus:(void *)starPrinterStatus :(u_int32_t)level;

// The other methods...

I read the framework documentation and I found this Objective-C code (that works perfectly):

@try
{
    [starPort getParsedStatus:&status :2];
}
@catch (PortException *exception){
    // Print error
}

So I tried to do something like this in Swift 2.1:

do{
    try starPort.getParsedStatus(status , 2)
}
catch is PortException{
    print("error")
}

But when the error occurs , the compiler stops the app, saying that I didn't catch that error:

2015-11-18 18:59:51.297 $$$$$[$$$$$:$$$$$] * Terminating app due to uncaught exception 'PortException', reason: 'Native GetParsedStatusEx failed' * First throw call stack: (0x2524a67b 0x36e76e17 0xa3af7 0x6f378 0x6f3d8 0x6fbc0 0x6fd10 0x6ef00 0x87bf8 0x87d80 0x29371559 0x293714e9 0x293594ff 0x29370e45 0x29370abf 0x2936947f 0x2933a561 0x29338bdb 0x2520dbff 0x2520d7ed 0x2520bb5b 0x2515f119 0x2515ef05 0x2e2fcac9 0x293a1f15 0x7f410 0x375e5873) libc++abi.dylib: terminating with uncaught exception of type PortException

I also tried something like this:

func doGPS() throws { starPort.getParsedStatus(status ,2) }
func test() {
   do {
      try doGPS()
   } catch is PortException{
      print("Error")
   } catch{
      print("WTF? : \(error)")
   }
}

test()

getting the same results...

So, how can I catch this error in Swift 2.1?

Here the complete code of SMPort.h https://github.com/gabebear/receiptbooth/blob/master/StarIO.framework/Headers/SMPort.h

This is a very good question! It's a legitimate issue that needs to be addressed. Unfortunately, you cannot achieve this at the moment. Here is why:

Swift exception handling has nothing to do with Exceptions. In fact, you cannot catch NSException, which is what's being raised in the Obj-C side. Swift catches NSError which is practically (in Swift) an Enum. Long story short, there are two patterns for handling errors in Objective-C: 1. raising NSException 2. Returning NSError

As I explained, you cannot handle errors raised using the first approach. So, you literally, have to modify your Objective-C code to comply with the second approach, using NSError.

Some good links:

https://www.bignerdranch.com/blog/error-handling-in-swift-2/ https://forums.developer.apple.com/thread/7582

So, thanks to @Peyman and @dan, I wrote two new Objective-C files where I implemented a new method (extending the SMPort class) that catches the error and returns an NSError

SMPort+Handler.h

#import <Foundation/Foundation.h>
#import <StarIO/SMPort.h>
@interface SMPort ( Handler )

- (BOOL)getParsedStatusThrowing:(void *)starPrinterStatus
                          level:(u_int32_t)level
               didFailWithError:(NSError **)error;

@end

SMPort+Handler.m

#import "SMPort+Handler.h"

@implementation SMPort ( Handler )


- (BOOL)getParsedStatusThrowing:(void *)starPrinterStatus
                          level:(u_int32_t)level
               didFailWithError:(NSError **)error
{
    @try
    {
        [self getParsedStatus:&starPrinterStatus :level];
        return TRUE;
    }
    @catch (PortException *exception){
        NSMutableDictionary* details = [NSMutableDictionary dictionary];

        // exemption.name in my case should be PortExeption
        [details setValue:exception.name forKey:NSLocalizedDescriptionKey];

        *error = [NSError errorWithDomain:@"somedomain" code:100 userInfo:details];
        return FALSE;
    }
}

@end

Now, I simply do (in my Swift code):

 do{
    //starPort.getParsedStatus(status ,2)
    try starPort.getParsedStatusThrowing(status, level: 2)

    defer{
       // Release the port in any case.
       SMPort.releasePort(starPort)
    }
 }catch{
    print(error)
    // do staff like return
 }

Thanks a lot!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM