# Is there a more optimal way to write the following encoding and then assign the result to a label?

Is there a more optimal (or concise) way to write the following:

I have to do some encoding and then assign the result to a label.

// encoding fix
NSString *correctStringTitle = [NSString stringWithCString: [[item objectForKey:@"main_tag"] cStringUsingEncoding:NSISOLatin1StringEncoding]
encoding: NSUTF8StringEncoding];

cell.titleLabel.text = [correctStringTitle capitalizedString];

-

Here is a marginally more concise solution:

cell.titleLabel.text = [NSString stringWithCString:[[item[@"main_tag"] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding].capitalizedString;


If I had to do this more than once I would consider hiding this complexity in an NSString category like:

@interface NSString(Utils)
-(NSString)stringWithLatinEncoding;
@end

@implementation NSString(Utils)

-(NSString)stringWithLatinEncoding
{
return [NSString stringWithCString:[self cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
}

@end


This will simplify your code to:

cell.titleLabel.text = ((NSString*)item[@"main_tag"]).stringWithLatinEncoding.capitalizedString;

-
Thanks. I have fixed it. –  Peter DeWeese Mar 5 '13 at 13:30