Created
November 5, 2012 16:00
-
-
Save pnc/4017972 to your computer and use it in GitHub Desktop.
(Possibly broken) camel-case / underscore inflection in Objective-C
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> | |
@interface NSDictionary (Inflector) | |
- (NSDictionary *)underscoredDictionaryWithPrefix:(NSString *)prefix andSuffix:(NSString *)suffix; | |
@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 "NSDictionary+Inflector.h" | |
#import "NSString+CamelCaseConversion.h" | |
@implementation NSDictionary (Inflector) | |
- (NSDictionary *)underscoredDictionaryWithPrefix:(NSString *)prefix andSuffix:(NSString *)suffix { | |
if (!prefix) { | |
prefix = @""; | |
} | |
if (!suffix) { | |
suffix = @""; | |
} | |
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:self.count]; | |
for (NSString *oldKey in self) { | |
NSString *newKey = [NSString stringWithFormat:@"%@%@%@", prefix, [oldKey fromCamelCaseToUnderscore], suffix]; | |
[result setValue:[self valueForKey:oldKey] forKey:newKey]; | |
} | |
return result; | |
} | |
@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 <Foundation/Foundation.h> | |
@interface NSString (CamelCaseConversion) | |
- (NSString *)fromCamelCaseToUnderscore; | |
- (NSString *)fromUnderscoreToCamelCase; | |
@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 "NSString+CamelCaseConversion.h" | |
@implementation NSString (CamelCaseConversion) | |
- (NSString *)fromCamelCaseToUnderscore { | |
NSScanner *scanner = [NSScanner scannerWithString:self]; | |
scanner.caseSensitive = YES; | |
NSString *builder = [NSString string]; | |
NSString *buffer = nil; | |
NSUInteger lastScanLocation = 0; | |
while ([scanner isAtEnd] == NO) { | |
if ([scanner scanCharactersFromSet:[NSCharacterSet lowercaseLetterCharacterSet] intoString:&buffer]) { | |
builder = [builder stringByAppendingString:buffer]; | |
if ([scanner scanCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet] intoString:&buffer]) { | |
builder = [builder stringByAppendingString:@"_"]; | |
builder = [builder stringByAppendingString:[buffer lowercaseString]]; | |
} | |
} | |
// If the scanner location has not moved, there's a problem somewhere. | |
if (lastScanLocation == scanner.scanLocation) return nil; | |
lastScanLocation = scanner.scanLocation; | |
} | |
return builder; | |
} | |
- (NSString *)fromUnderscoreToCamelCase { | |
NSArray *parts = [self componentsSeparatedByString:@"_"]; | |
NSMutableString *builder = [NSMutableString string]; | |
BOOL shouldUpperCase = NO; | |
for (NSString *part in parts) { | |
NSString *first = [part substringToIndex:1]; | |
NSString *rest = [part substringFromIndex:1]; | |
if (shouldUpperCase) { | |
first = [first uppercaseString]; | |
} | |
[builder appendFormat:@"%@%@", first, rest]; | |
shouldUpperCase = YES; | |
} | |
return builder; | |
} | |
@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 <Foundation/Foundation.h> | |
@interface NSString (CamelCaseConversion) | |
- (NSString *)fromCamelCaseToUnderscore; | |
- (NSString *)fromUnderscoreToCamelCase; | |
@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 "NSString+CamelCaseConversion.h" | |
@implementation NSString (CamelCaseConversion) | |
- (NSString *)fromCamelCaseToUnderscore { | |
NSScanner *scanner = [NSScanner scannerWithString:self]; | |
scanner.caseSensitive = YES; | |
NSString *builder = [NSString string]; | |
NSString *buffer = nil; | |
NSUInteger lastScanLocation = 0; | |
while ([scanner isAtEnd] == NO) { | |
if ([scanner scanCharactersFromSet:[NSCharacterSet lowercaseLetterCharacterSet] intoString:&buffer]) { | |
builder = [builder stringByAppendingString:buffer]; | |
if ([scanner scanCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet] intoString:&buffer]) { | |
builder = [builder stringByAppendingString:@"_"]; | |
builder = [builder stringByAppendingString:[buffer lowercaseString]]; | |
} | |
} | |
// If the scanner location has not moved, there's a problem somewhere. | |
if (lastScanLocation == scanner.scanLocation) return nil; | |
lastScanLocation = scanner.scanLocation; | |
} | |
return builder; | |
} | |
- (NSString *)fromUnderscoreToCamelCase { | |
NSArray *parts = [self componentsSeparatedByString:@"_"]; | |
NSMutableString *builder = [NSMutableString string]; | |
BOOL shouldUpperCase = NO; | |
for (NSString *part in parts) { | |
NSString *first = [part substringToIndex:1]; | |
NSString *rest = [part substringFromIndex:1]; | |
if (shouldUpperCase) { | |
first = [first uppercaseString]; | |
} | |
[builder appendFormat:@"%@%@", first, rest]; | |
shouldUpperCase = YES; | |
} | |
return builder; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment