Modify NSError localizedDescription

user3250560 picture user3250560 · Mar 11, 2014 · Viewed 9.9k times · Source

Do you guys know if there is a nice way to set/modify a NSError's localizedDescription after it has been instantiated other than recreating it? I didn't find any.

Answer

cacau picture cacau · Mar 12, 2014

The NSError class is - like many Cocoa classes - immutable.

However, the doc states for -localizedDescription that:

By default this method returns the object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code.

So just use errorWithDomain:code:userInfo: to create a new instance or copy and supply an appropriate user info.

Here's another nice introduction to the NSError class.