dod*_*onl 13 cocoa-touch nsstring iphone-sdk-3.0 nsuinteger
我在NSString中有一个数字@"15".我想把它转换成NSUInteger,但我不知道怎么做...
squ*_*art 26
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];
Run Code Online (Sandbox Code Playgroud)
如果你真的想要一个NSUInteger,只需要它,但你可能想要预先测试它.
Zac*_*nis 19
NSUInteger当前选择的答案不正确.正如Corey Floyd指出对所选答案的评论,如果该值大于INT_MAX,这将不起作用.更好的方法是使用NSNumber,然后使用NSNumber上的一种方法来检索您感兴趣的类型,例如:
NSString *str = @"15"; // Or whatever value you want
NSNumber *number = [NSNumber numberWithLongLong: str.longLongValue];
NSUInteger value = number.unsignedIntegerValue;
Run Code Online (Sandbox Code Playgroud)
所有这些答案在 64 位系统上都是错误的。
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.
Run Code Online (Sandbox Code Playgroud)
使用 9223372036854775808 进行测试,它不适合已签名的long long.
| 归档时间: |
|
| 查看次数: |
16102 次 |
| 最近记录: |