Search code examples
iosobjective-cencodingcstring

How to resolve a NULL cString crash


I'm getting a crash with the following encoding fix I'm trying to implement:

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

    cell.titleLabel.text = [correctStringTitle capitalizedString];

my crash log output states:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithCString:encoding:]: NULL cString'

thanks for any help


Solution

  • You seem to think that a double conversion is necessary in order to get the correct result. It isn't, nor is it possible (that's why the call to cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL). Just leave this part out and assign to correctStringTitle directly.