Capturing POSIX Error Codes from Network Framework for macOS 10.15+ and iOS 12+

Hi everyone,

I'm currently working on a project that involves using the Network framework on macOS 10.15 and iOS 12. While implementing error handling for my network connections, I encountered a warning about the conformance of 'NWError' to 'CustomNSError', which is only available in macOS 13.3 or newer.

Here's the warning message I received while compiling the code:

Warning: conformance of 'NWError' to 'CustomNSError' is only available in macOS 13.3 or newer


self.vConnection = try NWConnection (to: self.vBaseSocketProperties!.uEndpoint!, using: self.vBaseSocketProperties!.uParamters!)    
            
 self.vConnection?.stateUpdateHandler = { connectionState in
       
       switch connectionState {
              case .failed(let err):
                   error_code = err.errorCode
                //Below all the other cases are also handled.
                }

Replies

You don’t need to go through CustomNSError to get the Posix error code. Rather, get it from the NWError directly:

switch error {
case .posix(let code): … use `code` …
… other cases …
}

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"