NSError
declares no setters for its properties. From this you can deduce that it probably is not meant to be modified after it has been created. I could see writing a category on NSError
that would create an error out of another error but with a different value for the NSLocalizedDescriptionKey
key.
Modify NSError localizedDescription
-
13-06-2023 - |
Question
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.
Solution 2
OTHER TIPS
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.
If you create your NSError manually specifying the userInfo
, then you can rewrite this dictionary's value for key NSLocalizedDescriptionKey
. Just cast error.userInfo
to NSMutableDictionary or whichever class you used when created NSError object