Wednesday, September 2, 2009

A Camel Case Category

I recently needed to convert a Core Data attribute name, which uses camel case, so they could be displayed as capitalized words for use in a label. This would give me the ability to label rows in a Core Data backed detail editing view without having to manually specify the labels.

I decided not to use this option because I would have lost the ability to localize my application since my attribute names are always and only in English. But, I thought I'd throw this category up for anyone who might need it. This functionality could come in useful, for example, when building a Core Data developer utility.

NSString-CamelCase.h
#import <Foundation/Foundation.h>


@interface NSString(CamelCase)
-(NSString *)stringByConvertingCamelCaseToCapitalizedWords;
@end


NSString-CamelCase.h
#import "NSString-CamelCase.h"

@implementation NSString(CamelCase)
-(NSString *)stringByConvertingCamelCaseToCapitalizedWords {

NSMutableString *ret = [NSMutableString string];

for (NSUInteger i = 0; i < [self length]; i++) {
unichar oneChar = [self characterAtIndex:i];
if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:oneChar])
[ret appendFormat:@" %C", oneChar];
else
[ret appendFormat:@"%C", oneChar];
}

return [[ret capitalizedString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

@end





6 comments:

K. A. Barber said...

I was asked to add camelization to an xslt based code generator I wrote for my current employer about a year ago. It allows for the automated creation C++ classes and sql stored procedures that match our current coding standard with full commenting and layout support. Speeds up development quite a bit. I really like this reverse camelcaser. Thanks for sharing.

Dave said...

This seems to be begging for a regular expression solution.

Jeff LaMarche said...

Dave:

Yeah, it does seem like it a great situation for regular expressions. Except regular expressions don't have language-level support in Objective-C, so it would have actually taken quite a bit more and harder-to-read code to implement this using regular expressions and it would have relied on external dependencies outside of my control, making it not a very easy to package up for generic use.

The end result would have been more code that takes more processor cycles to do the job.

It's ten lines of code. If we had language-level support like Perl or Ruby, and I could have replaced those ten lines with one or two, it would have been a good idea to do it. That's simply not the case in Objective-C.

I don't see regular expressions being a substantially better solution in this case. The only built-in regular expression support is in NSPredicate and using libicucore.dylib would entail conversions to and from NSString to UTF8 and the use of an external library that Apple has specifically documented as being unsupported for direct application use. Using a third-party library like PCRE or RegExKit would involve licensing issues and add dependencies.

Dave said...

Speaking of Ruby... These are the kinds of issues that make me doubt we'll ever be able to use something like MacRuby/RubyCocoa for serious iPhone development. It would be cool to write the above in a few lines of Ruby, but at what cost? An app so slow nobody would use it?

Jeff LaMarche said...

Honestly, I don't know much about RubyCocoa. Although I like Ruby a lot, I don't see the point in coercing a framework designed specifically for one language to work for another one. Just like I wouldn't be interested in using Cocoa on Rails.

I don't even know if there's a Ruby interpreter on non-jailbroken iPhones and have idea whether it would be slow or not.

I do admit that language-level regular expression support is at the top of my list for Objective-C 3.0. I doubt we'll get it, but I'm going to hold out hope.

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels