¿Cómo se convierte NSIntegeral NSStringtipo de datos?
Intenté lo siguiente, donde el mes es un NSInteger:
NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
¿Cómo se convierte NSIntegeral NSStringtipo de datos?
Intenté lo siguiente, donde el mes es un NSInteger:
NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
Respuestas:
NSIntegers no son objetos, a los que los envía long, para que coincidan con la definición actual de las arquitecturas de 64 bits:
NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];
[@(integerValue) stringValue]Es un enfoque más limpio.
Obj-C way =):
NSString *inStr = [@(month) stringValue];
An NSIntegertiene el método stringValueque puede usarse incluso con un literal
NSString *integerAsString1 = [@12 stringValue];
NSInteger number = 13;
NSString *integerAsString2 = [@(number) stringValue];
Muy simple. ¿No es así?
var integerAsString = String(integer)
%zdfunciona para NSIntegers ( %tupara NSUInteger) sin conversiones y sin advertencias en arquitecturas de 32 bits y 64 bits. No tengo idea de por qué esta no es la " forma recomendada ".
NSString *string = [NSString stringWithFormat:@"%zd", month];
Si está interesado en por qué esto funciona, vea esta pregunta .
Manera fácil de hacer:
NSInteger value = x;
NSString *string = [@(value) stringValue];
Aquí los @(value)conversos lo dado NSIntegera un NSNumberobjeto para el que se puede llamar a la función requerida, stringValue.
Al compilar con soporte para arm64, esto no generará una advertencia:
[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];
También puedes probar:
NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: @"%ld", month];
Format specifies type 'int' but the argument has type 'NSInteger *'(aka 'int *'). En lugar de acuerdo con la documentación de Apple , fui conNSString *inStr = [NSString stringWithFormat:@"%d", (int)month];