Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(Possibly broken) camel-case / underscore inflection in Objective-C
#import <Foundation/Foundation.h>
@interface NSDictionary (Inflector)
- (NSDictionary *)underscoredDictionaryWithPrefix:(NSString *)prefix andSuffix:(NSString *)suffix;
@end
#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
#import <Foundation/Foundation.h>
@interface NSString (CamelCaseConversion)
- (NSString *)fromCamelCaseToUnderscore;
- (NSString *)fromUnderscoreToCamelCase;
@end
#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
#import <Foundation/Foundation.h>
@interface NSString (CamelCaseConversion)
- (NSString *)fromCamelCaseToUnderscore;
- (NSString *)fromUnderscoreToCamelCase;
@end
#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
You can’t perform that action at this time.