Pregunta

Por ejemplo cuando se pasa un mensaje value a una instancia NSInteger como tal

[a value] provoca una EXC_BAD_ACCESS.

Entonces, ¿cómo convertir un NSInteger a int?

Si se trata de sólo números pequeños pertinentes

¿Fue útil?

Solución

Ta da:

NSInteger myInteger = 42;
int myInt = (int) myInteger;

NSInteger es nada más que un int 32/64 bits. (Se utilizará el tamaño adecuado en función de qué sistema operativo / plataforma que se está ejecutando)

Otros consejos

Si quieres hacer esto en línea, simplemente tire la NSUInteger o NSInteger a un int:

int i = -1;
NSUInteger row = 100;
i > row // true, since the signed int is implicitly converted to an unsigned int
i > (int)row // false

No estoy seguro acerca de las circunstancias en las que necesita para convertir un NSInteger a un int.

NSInteger es sólo un typedef:

NSInteger Se utiliza para describir un número entero independientemente de si usted está construyendo una de 32 bits o un sistema de 64 bits.

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

Puede utilizar NSInteger cualquier lugar se utiliza un int sin convertirlo.

utiliza comúnmente en UIsegmentedControl, "error" aparece cuando la compilación de 64 bits en lugar de 32 bits, forma fácil para que no pase a una nueva variable es utilizar estos consejos, añadir (int)

[_monChiffre setUnite:(int)[_valUnites selectedSegmentIndex]];

en lugar de:

[_monChiffre setUnite:[_valUnites selectedSegmentIndex]];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top