Tengo un número en NSString @"15"
. Quiero convertir esto en NSUInteger, pero no sé cómo hacer eso ...NSString a NSUInteger
12
A
Respuesta
25
NSString *str = @"15";
// Extract an integer number, returns 0 if there's no valid number at the start of the string.
NSInteger i = [str integerValue];
Si realmente quiere un NSUInteger, simplemente fundido esto, pero es posible que desee probar el valor de antemano.
0
se puede tratar con [string longLongValue]
o [string intValue]
..
17
La respuesta actualmente elegida es incorrecta para NSUInteger. Como Corey Floyd señala un comentario sobre la respuesta seleccionada, esto no funcionará si el valor es mayor que INT_MAX. Una mejor manera de hacer esto es utilizar NSNumber y luego usando uno de los métodos en NSNumber para recuperar el tipo que le interesa, por ejemplo .:
NSString *str = @"15"; // Or whatever value you want
NSNumber *number = [NSNumber numberWithLongLong: str.longLongValue];
NSUInteger value = number.unsignedIntegerValue;
1
Todas estas respuestas son incorrectas en un sistema de 64 bits.
NSScanner *scanner = [NSScanner scannerWithString:@"15"];
unsigned long long ull;
if (![scanner scanUnsignedLongLong:&ull]) {
ull = 0; // Or handle failure some other way
}
return (NSUInteger)ull; // This happens to work because NSUInteger is the same as unsigned long long at the moment.
prueba con 9223372036854775808, que no cabe en un long long
firmado.
Cuestiones relacionadas
- 1. ¿Cómo se convierte un NSUInteger en un NSString?
- 2. ¿Convertir NSInteger a NSUInteger?
- 3. Xcode 4: nombre de tipo desconocido 'NSUInteger'; ¿Te refieres a 'NSUInteger'?
- 4. Cómo almacenar un NSUInteger usando NSCoding?
- 5. cuestión semántica: puntero incompatible a entero conversión enviar 'NSUInteger *' (también conocido como 'unsigned int *') al parámetro de tipo 'NSUInteger'
- 6. NSNotFound, NSInteger, NSUInteger y NSRange.location
- 7. Cómo agregar un NSString a otro NSString
- 8. ¿Agregar NSString a NSURL?
- 9. NSString a NSURL?
- 10. Convertir char a NSString
- 11. NSString (hex) a bytes
- 12. Casting NSObject a NSString
- 13. ¿Convierte NSString a NSInteger?
- 14. NSString a la ecuación
- 15. NSString a NSDate
- 16. NSURLRequest a NSString
- 17. NSString a NSArray
- 18. Convertir NSDate a NSString
- 19. BOOL a NSString
- 20. NSString a NSURL
- 21. convertir NSDictionary a NSString
- 22. Diferencia entre int, NSInteger y NSUInteger
- 23. uso de NSString estático frente a constantes NSString en línea
- 24. Objective-C: - [NSString wordCount]
- 25. Cómo convertir a NSString bytes
- 26. iPhone SDK NSString A NSDate
- 27. Conversión de CGAffineTransform a NSString
- 28. Convierte de char * a NSString?
- 29. conversión de NSDecimalNumber a NSString
- 30. Convirtiendo un NSString * a char?
Para solicitar soporte para leer valores sin signo de NSString, visite http://bugreport.apple.com y archive una trampa de radar: // 2264733 contra el componente 'Fundación | X'. –