O'Reilly logo

Protocol-Oriented Programming with Swift by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Error handling with NSError

The NSError class encapsulates information about an error into a single object. It consists of an error domain, a domain-specific error code, and a user information dictionary that contains specific information about the error. When we use NSError in Swift we add an NSError inout parameter of the NSErrorPointer type to our method's parameter list.

As a general rule we should not use an NSError instance to indicate whether an error occurred or not. Our methods should instead return either a nil or a Boolean false to indicate a failure. We can then use the NSError instance to give details about the error that occurred.

Let's look at how we can use the NSError class in our code. We will want to begin by defining our error ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required