Skip to content
tezvyn:

Swift Errors and the NSError Bridge

Source: github.comMediumHow cards are made

Swift's modern Error protocol and Objective-C's NSError are different, but Swift automatically "bridges" them. This is key when calling older Apple APIs, which throw NSErrors.

Why it exists

Swift was designed for seamless interoperability with existing Objective-C codebases. Objective-C uses a class, NSError, for error handling, typically passed as an out-parameter (NSError **). Swift introduced the more modern Error protocol and do-try-catch syntax, requiring a robust "bridge" to make the two systems communicate effectively.

The mental model

Think of the NSError bridge as a translator at the border between Swift and Objective-C. When a Swift function calls an old Objective-C method that can fail, the translator converts the potential NSError object into a thrown Swift Error. When you catch it, you're handling what looks like a native Swift error, but it's often an NSError wearing a Swift Error costume.

How it works

Swift automatically imports Objective-C methods with an NSError** parameter as functions that throw. When you call one and an error occurs, your catch block receives a generic Error object. To get meaningful details, you must cast this object to NSError. Once cast, you can inspect its three key properties: domain (a string identifying the error's source, like NSURLErrorDomain), code (an integer for the specific error), and userInfo (a dictionary for extra context).

When to use it

You'll interact with this bridge constantly when using Apple's platform frameworks like Foundation, UIKit, or AppKit. Core operations like file I/O with FileManager or network requests with URLSession all use NSError under the hood. Any time you catch an error from a system API, you should attempt to cast it to NSError to properly diagnose and handle the failure.

When not to use it

For pure Swift code—your own application logic or libraries without Objective-C dependencies—you should define custom error types, typically enums conforming to the Error protocol. This approach is more type-safe and expressive. The NSError bridge is a tool for interoperability, not the preferred model for designing new error-handling logic in Swift.

One canonical example

A URLSession data task is a classic case. The completion handler provides an optional Error. If an error exists, it's an NSError. You would cast it to inspect its code property and compare it against predefined constants like NSURLErrorNotConnectedToInternet or NSURLErrorTimedOut to determine the specific cause of the network failure and update the UI accordingly.

Interview question

When handling an error from a system API in Swift, why might you cast the caught Error object to NSError?

  • a.To access specific error details like domain, code, and userInfo.Correct
  • b.To allow the error to be rethrown to an Objective-C caller.
  • c.To ensure the error conforms to the Swift Error protocol.
  • d.To convert it into a custom Swift error type for better type safety.
Why?

The card states that casting to NSError is necessary "To get meaningful details" such as its domain, code, and userInfo. Option C is incorrect because the bridge automatically makes the NSError conform to Swift's Error protocol; casting is for detail extraction, not conformance.

Just read this? Test yourself on what you have been reading.

Read the original → github.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.

See open roles