Created
October 25, 2012 09:48
-
-
Save tatey/3951715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <UIKit/UIKit.h> | |
@interface ErrorFormatter : NSObject | |
@property (strong, nonatomic) NSError *error; | |
- (id)initWithError:(NSError *)error; | |
- (UIAlertView *)alert; | |
- (NSString *)message; | |
- (NSString *)title; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ErrorFormatter.h" | |
@implementation ErrorFormatter | |
- (id)initWithError:(NSError *)error { | |
self = [self init]; | |
if (self) { | |
self.error = error; | |
} | |
return self; | |
} | |
- (UIAlertView *)alert { | |
return [[UIAlertView alloc] initWithTitle:[self title] | |
message:[self message] | |
delegate:nil | |
cancelButtonTitle:NSLocalizedString(@"controls.ok", nil) | |
otherButtonTitles:nil]; | |
} | |
- (NSString *)message { | |
return [[NSBundle mainBundle] localizedStringForKey:[self keyWithType:@"message"] | |
value:[self defaultMessage] | |
table:nil]; | |
} | |
- (NSString *)title { | |
return [[NSBundle mainBundle] localizedStringForKey:[self keyWithType:@"title"] | |
value:[self defaultTitle] | |
table:nil]; | |
} | |
- (NSString *)defaultMessage { | |
if ([self.error localizedRecoverySuggestion]) { | |
return [self.error localizedRecoverySuggestion]; | |
} else { | |
return [self.error localizedDescription]; | |
} | |
} | |
- (NSString *)defaultTitle { | |
if ([self.error localizedFailureReason]) { | |
return [self.error localizedFailureReason]; | |
} else { | |
return NSLocalizedString(@"errors.title", nil); | |
} | |
} | |
- (NSString *)keyWithType:(NSString *)type { | |
return [NSString stringWithFormat:@"errors.%@.%d.%@", self.error.domain, self.error.code, type]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error { | |
ErrorFormatter *formatter = [[ErrorFormatter alloc] initWithError:error]; | |
UIAlertView *alert = [formatter alert]; | |
[alert show]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Errors | |
"errors.title" = "Error"; | |
"errors.kCLErrorDomain.0.title" = "Unknown Location"; | |
"errors.kCLErrorDomain.0.message" = "Couldn't get a good reading. Long tunnel?"; | |
"errors.kCLErrorDomain.2.title" = "No Internet"; | |
"errors.kCLErrorDomain.2.message" = "Internet is unavailable or an error occured"; | |
"errors.kCLErrorDomain.8.title" = "No results found"; | |
"errors.kCLErrorDomain.8.message" = ""; | |
"errors.kCLErrorDomain.9.title" = "No results found"; | |
"errors.kCLErrorDomain.8.message" = ""; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ErrorFormatter
Thin wrapper around
NSError
which lets you define custom strings or defaults toNSError
localized strings.Features
Localizable.strings
.NSLocalizedString(@"errors.title", nil)
localizedRecoverySuggestion
orlocalizedDescription
.Screenshot