Skip to content

Instantly share code, notes, and snippets.

@sebk
Last active December 19, 2015 02:38
Show Gist options
  • Save sebk/5884099 to your computer and use it in GitHub Desktop.
Save sebk/5884099 to your computer and use it in GitHub Desktop.
NSError category to create a localized NSError instance. Created by Jan Rose.
#import <Foundation/Foundation.h>
@interface NSError(LocalizedError)
/**
Utility method to create a localized NSError with the given error-domain, error-code and description text.
*/
+ (NSError*)localizedErrorWithDomain:(NSString*)domain code:(NSInteger)code andDescription:(NSString*)description;
@end
#import "NSError+LocalizedError.h"
@implementation NSError (LocalizedError)
+ (NSError*)localizedErrorWithDomain:(NSString*)domain code:(NSInteger)code andDescription:(NSString*)description
{
NSMutableArray* objArray = [NSMutableArray arrayWithCapacity:1];
NSMutableArray* keyArray = [NSMutableArray arrayWithCapacity:1];
[objArray addObject:description];
[keyArray addObject:NSLocalizedDescriptionKey];
NSDictionary *eDict = [NSDictionary dictionaryWithObjects:objArray
forKeys:keyArray];
return [NSError errorWithDomain: domain
code: code
userInfo: eDict];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment