I am attempting to bridge an objective-c SDK to a swift implementation. I have an example of what the implementation would be in objective-c but don't seem to be able to implement this in a workable solution in swift. Enum in objective-c header file below:
typedef NS_ENUM(NSUInteger, MTSCRATransactionStatus)
{
TRANS_STATUS_OK,
TRANS_STATUS_START,
TRANS_STATUS_ERROR
};
Objective-C usage:
- (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;
}
}
I would need to change this into a swift equivalent. However I cannot get this work and have tried different casting of the status.
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
}
}
While this may appear to be a duplicate of How to use Objective-C enum in Swift
The main issues is around this line of code:
[status intValue]
What would the Swift equivalent be given the NS_ENUM.
EDIT SOLUTION
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
}
}
}
}