tezvyn:

Swift Errors and the NSError Bridge

Source: github.comintermediate

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 `NSError`s.

Swift's `do-try-catch` unifies error handling, but it bridges two worlds: native Swift `Error`s and Objective-C's `NSError`. When you call a Cocoa API, Swift translates its `NSError` into a value you can `catch`. To understand the failure, you must cast this generic `Error` back to `NSError`. The main footgun is forgetting this cast; without it, you can't access the essential `code` and `domain` properties that specify the exact problem, like a network timeout versus a file-not-found error.

Read the original → github.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Swift Errors and the NSError Bridge · Tezvyn