Changing localization NSError localizedDescription

Do you guys know if there is a good way to set / modify a localized NSError declaration after it has been created, except to recreate it? I did not find.

+8
ios cocoa-touch cocoa nserror
source share
3 answers

NSError does not declare any settings for its properties. From this you can conclude that it probably will not be changed after its creation. I could see a category entry on NSError that would create an error from another error, but with a different value for the NSLocalizedDescriptionKey key.

+7
source share

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

However, the doc claims for -localizedDescription that:

By default, this method returns an object in the user information dictionary for the key NSLocalizedDescriptionKey. If the user information dictionary does not contain a value for NSLocalizedDescriptionKey, the default value of the string is constructed from the domain and code.

Therefore, simply use errorWithDomain:code:userInfo: to create a new instance or copy and provide relevant user information.

Here is another good introduction to the NSError class.

+15
source share

If you create your NSError manually by specifying userInfo , you can overwrite this dictionary value for the NSLocalizedDescriptionKey key. Just enter error.userInfo in NSMutableDictionary or depending on which class you used when creating the NSError object

+5
source share

All Articles