Last active
August 29, 2015 14:14
-
-
Save joeljfischer/67bd1d4da444e948cb5d to your computer and use it in GitHub Desktop.
SDLEnum Redesign Example
This file contains hidden or 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> | |
@interface SDLEnum : NSObject | |
+ (NSDictionary *)map; | |
+ (NSUInteger)transformString:(NSString *)stringValue; | |
+ (NSString *)transformEnum:(NSUInteger)enumValue; | |
@end |
This file contains hidden or 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 "SDLEnum.h" | |
@implementation SDLEnum | |
+ (NSDictionary *)map { | |
@throw [NSException exceptionWithName:@"SDLAbstractMethodException" reason:[NSString stringWithFormat:@"Cannot call %s on %@, must be called on a subclass", __FUNCTION__, self.class] userInfo:nil]; | |
} | |
+ (NSUInteger)transformString:(NSString *)stringValue { | |
__block NSNumber *result = nil; | |
[[self map] enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, NSString *obj, BOOL *stop) { | |
if ([obj isEqualToString:stringValue]) { | |
result = key; | |
*stop = YES; | |
} | |
}]; | |
return result.unsignedIntegerValue ?: NSNotFound; | |
} | |
+ (NSString *)transformEnum:(NSUInteger)enumValue { | |
return [self map][@(enumValue)]; | |
} | |
- (id)objectAtIndexedSubscript:(NSUInteger)idx { | |
return [self.class transformEnum:idx]; | |
} | |
- (id)objectForKeyedSubscript:(id<NSCopying, NSObject>)key { | |
if ([key isKindOfClass:[NSString class]]) { | |
return @([self.class transformString:(NSString *)key]); | |
} | |
return nil; | |
} | |
@end |
This file contains hidden or 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 "SDLEnum.h" | |
typedef NS_ENUM(NSUInteger, SDLAppHMIType) { | |
SDLEnumAppHMITypeDefault, | |
SDLEnumAppHMITypeCommunication, | |
SDLEnumAppHMITypeMedia, | |
SDLEnumAppHMITypeMessaging, | |
SDLEnumAppHMITypeNavigation, | |
SDLEnumAppHMITypeInformation, | |
SDLEnumAppHMITypeSocial, | |
SDLEnumAppHMITypeBackgroundProcess, | |
SDLEnumAppHMITypeTesting, | |
SDLEnumAppHMITypeSystem | |
}; | |
@interface SDLEnumAppHMIType : SDLEnum | |
@end |
This file contains hidden or 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 "SDLEnumAppHMIType.h" | |
@implementation SDLEnumAppHMIType | |
+ (NSDictionary *)map { | |
static NSDictionary *dict; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
dict = @{@(SDLEnumAppHMITypeDefault): @"DEFAULT", | |
@(SDLEnumAppHMITypeCommunication): @"COMMUNICATION"}; | |
}); | |
return dict; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment