Platformsios 5.0, requires ARC

Sane NSError

Creating NSError can often be confusing. According to Apple’s guidelines and how NSError is used by community it is safe to understand the error keys like this:

  • NSLocalizedDescriptionKey => What went wrong, and why the error occurred
  • NSLocalizedFailureReasonErrorKey => Why the error occurred
  • NSLocalizedRecoverySuggestionErrorKey => What can be done to circumvent or fix the error

Example code

Creating errors is much easier and ambiguous now:

NSError *error = [NSError errorWithDomain:ZYZUserServiceDomain
                      whatWentWrongAndWhy:@"Could not log in due to incorrect username or password"
                                      why:@"Username or password is incorrect"
                               suggestion:@"Verify username and password is typed correctly"];

Handy properties:

@property (nonatomic, readonly) NSString *whatWentWrongAndWhy;  // NSLocalizedDescriptionKey
@property (nonatomic, readonly) NSString *why;                  // NSLocalizedFailureReasonErrorKey
@property (nonatomic, readonly) NSString *suggestion;           // NSLocalizedRecoverySuggestionErrorKey

Makes it very trivial to get the relevant information:

NSLog(@"Could not log in: %@", error.why);


Q: Why is there one key with both "what" and "why"? We already answer "why" in the "FailureReason"-key.
A: This is how NSError is designed – which is great since it enables developers to choose between a well articulated and verbose sentence and a more simple one.


NSError, easy, setup, explained, how to construct NSError, NSLocalizedDescriptionKey, NSLocalizedFailureReasonErrorKey, NSLocalizedRecoverySuggestionErrorKey, domain, code.

Who’s behind this?

You can reach me on twitter as @hfossli. a company situated in Oslo, Norway.

Agens | Digital craftsmanship

Latest podspec

    "name": "SaneNSError",
    "version": "1.0",
    "summary": "Unambiguous interface for NSError.",
    "homepage": "",
    "license": "MIT",
    "platforms": {
        "ios": "5.0"
    "requires_arc": true,
    "authors": {
        "Hu00e5vard Fossli": "[email protected]"
    "source": {
        "git": "",
        "tag": "1.0"
    "frameworks": "Foundation",
    "source_files": "Source/**/*.{h,m}"

Pin It on Pinterest

Share This