TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Håvard Fossli.
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:
Creating errors is much easier and ambiguous now:
NSError *error = [NSError errorWithDomain:ZYZUserServiceDomain
code:0
whatWentWrongAndWhy:@"Could not log in due to incorrect username and/or password"
why:@"Username and/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.
You can reach me on twitter as @hfossli. Agens.no a company situated in Oslo, Norway.