2011-10-29 33 views
12

Duplicar posibles:
Convert from char* to NSString?Convertir char a NSString

he char myChar = 'r'

¿Cómo convertirlo en un NSString?

NSString* string = @"r"; 
+1

CHAR = 'r'' ?? – Mob

+1

posible duplicado de [Convertir de char * a NSString?] (Http://stackoverflow.com/questions/6325993/) y [Convertir char a NSString] (http://stackoverflow.com/questions/5134663/) y [ Cómo convertir cadena C a NSString] (http://stackoverflow.com/questions/3481396/) y [Char * en NSString] (http://stackoverflow.com/questions/6104357/) –

+2

Esto no es un duplicado de la pregunta "[Convertir de char * a NSString?] (http://stackoverflow.com/questions/6325993/convert-from-char-to-nsstring)". En los lenguajes basados ​​en C, un 'char *' es una cadena (una matriz de caracteres) mientras que 'char' es solo un valor de 1 byte. Como tal, las soluciones son drásticamente diferentes. –

Respuesta

49
char myChar = 'r'; 
NSString* string = [NSString stringWithFormat:@"%c" , myChar]; 
4
char myChar = 'a'; 
NSString * string = [NSString stringWithFormat:@"%c", myChar]; 
1

NSString* string = @"r"; funciona bien.

Si el carbón no es un literal a continuación, utilizar:

NSString* string = [NSString stringWithFormat:@"%c" , myChar];