1
\$\begingroup\$

I have to do some encoding and then assign the result to a label. Is there a more optimal (or concise) way to write this?

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

cell.titleLabel.text = [correctStringTitle capitalizedString];
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

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;
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.