Created
April 9, 2015 15:47
-
-
Save thefotes/89f7b0407699073d8815 to your computer and use it in GitHub Desktop.
Camel Case String 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
// | |
// NSString+CamelCase.m | |
// CamelCase | |
// | |
// Created by Peter Foti on 4/9/15. | |
// Copyright (c) 2015 Peter Foti. All rights reserved. | |
// | |
#import "NSString+CamelCase.h" | |
@implementation NSString (CamelCase) | |
- (NSString *)camelCaseWithDelimiter:(NSString *)delimiter | |
{ | |
NSString *normalized = [self lowercaseString]; | |
NSArray *components = [normalized componentsSeparatedByString:delimiter]; | |
NSMutableString *camelCasedString = [NSMutableString new]; | |
for (NSString *component in components) { | |
if (![component isEqualToString:delimiter]) { | |
if ([component isEqualToString:[components firstObject]]) { | |
[camelCasedString appendString:component]; | |
} else { | |
[camelCasedString appendString:[component capitalizedString]]; | |
} | |
} | |
} | |
return camelCasedString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment