0

我正在尝试将 Objective-c SDK 连接到 swift 实现。我有一个在objective-c中实现的例子,但似乎无法在一个可行的解决方案中迅速实现这一点。以下objective-c头文件中的枚举:

typedef NS_ENUM(NSUInteger, MTSCRATransactionStatus)
{
    TRANS_STATUS_OK,
    TRANS_STATUS_START,
    TRANS_STATUS_ERROR
};

Objective-C 用法:

- (void)onDataEvent:(id)status
{
     //[self clearLabels];
     switch ([status intValue]) {
           case TRANS_STATUS_OK:
                NSLog(@"TRANS_STATUS_OK");
                break;
           case TRANS_STATUS_ERROR:
                NSLog(@"TRANS_STATUS_ERROR");
                break;
           default:
           break;
     } 
 }

我需要将其更改为 swift 等价物。但是我无法完成这项工作,并尝试了不同的状态转换。

func trackDataReady (notification: NSNotification) {

    let status = notification.userInfo?["status"] as? NSNumber

    self.performSelector(onMainThread: 
     #selector(CardReaderClass.onDataEvent), with: status, 
     waitUntilDone: true)

}


func onDataEvent (status: Any) {

    switch status.intValue {

        case .TRANS_STATUS_START:
            print("TRANS_STATUS_START")
            break
        case .TRANS_STATUS_OK:
            print("TRANS_STATUS_OK")
            returnData()
            break
        case .TRANS_STATUS_ERROR:
            print("TRANS_STATUS_ERROR")
            returnError()
            break
    }

}

虽然这可能看起来是如何在 Swift 中使用 Objective-C 枚举的副本

主要问题在于这行代码:

[status intValue]

给 NS_ENUM 的 Swift 等价物会是什么?

编辑 解决方案

func onDataEvent (status: Any) {

    if let statusInt = status as? Int {
        if let newStatus = MTSCRATransactionStatus(rawValue: UInt(statusInt)) {

            switch newStatus {
            case .TRANS_STATUS_START:
                print("TRANS_STATUS_START")
                break
            case .TRANS_STATUS_OK:
                print("TRANS_STATUS_OK")
                returnData()
                break
            case .TRANS_STATUS_ERROR:
                print("TRANS_STATUS_ERROR")
                returnError()
                break
            }

        }
    }

}
4

0 回答 0