NordicSemiconductor / IOS-nRF-Connect-Device-Manager

A mobile management library for devices supporting nRF Connect Device Manager.
https://www.nordicsemi.com/Software-and-tools/Software/nRF-Connect-SDK
Apache License 2.0
86 stars 37 forks source link

[Question] How to tell what's the integer error-code when an upload fails? #198

Closed ksidirop-laerdal closed 3 months ago

ksidirop-laerdal commented 5 months ago

I'm new to swift so please bare with me. I noticed that in McuManager.swift we have this piece of code:

extension McuMgrReturnCode: CustomStringConvertible {

    public var description: String {
        switch self {
        case .ok:
            return "OK (0)"
        case .unknown:
            return "Unknown (1)"
        case .noMemory:
            return "No Memory (2)"
        case .inValue:
            return "In Value (3)"
        case .timeout:
            return "Timeout (4)"
        case .noEntry:
            return "No Entry (5)"
        case .badState:
            return "Bad State (1)"
        default:
            return "Unrecognized (\(rawValue))"
        }
    }
}

When an upload fails the following callback gets triggered:

extension FooFileUploader: FileUploadDelegate {

   public func uploadDidFail(with error: Error) {
        //  ...
   }

}

How can I get the integer error-code (McuMgrReturnCode) from the 'error' parameter? Is it even possible or is it a lost cause? Appreciate any insights.

dinesharjani commented 3 months ago

You can typecast the error.

So something like

if let mcuMgrError = error as? McuMgrError { // here mcuMgrError is of type McuMgrError which can be either case returnCode or case groupCode }