Skip to content

Instantly share code, notes, and snippets.

@thefotes
Created April 9, 2015 15:47
Show Gist options
  • Save thefotes/89f7b0407699073d8815 to your computer and use it in GitHub Desktop.
Save thefotes/89f7b0407699073d8815 to your computer and use it in GitHub Desktop.
Camel Case String in Objective-C
//
// 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