我在NSString @"15"
中有一個數字。我想將其轉換爲NSUInteger,但我不知道該怎麼做......NSString到NSUInteger
12
A
回答
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];
如果你真的想要一個NSUInteger,只投它,但您可能需要事先測試該值。
0
你可以用[string longLongValue]
或[string intValue]
嘗試..
17
當前選擇的答案對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;
1
所有這些答案都是錯的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.
測試與9223372036854775808,這將不適合在簽署long long
。
相關問題
- 1. 如何將NSUInteger轉換爲NSString?
- 2. 我想使用characterAtIndex:(NSUInteger)NSString類的索引方法
- 3. MPMediaItemCollection Count - NSUInteger
- 4. ios NSUinteger輸入'id'
- 5. NSNotFound,NSInteger,NSUInteger和NSRange.location
- 6. 將NSUInteger除以100
- 7. Xcode 4:未知類型名稱'NSUInteger';你的意思是'NSUInteger'嗎?
- 8. nsdata到nsstring到nsdata
- 9. ReConvert NSString到NSArray
- 10. NSString到UIImage
- 11. NSString到NSArray
- 12. NSString到NSData中
- 13. NSlog()到NSString
- 14. HTML到NSString
- 15. 分配一個NSUInteger IndexBuffer
- 16. 如何將NSUInteger傳入NSDictionary
- 17. 指向NSUInteger的指針
- 18. 將CFIndex轉換爲NSUInteger?
- 19. NSUInteger小於0,爲什麼?
- 20. 將NSString從NSMutableArray連接到NSString
- 21. 另一個「NSUInteger *」(又名「無符號整數*」)與「NSUInteger」(又名「無符號整型」)
- 22. 轉換NSString到NSDate
- 23. NSString日期到NSDate
- 24. NSString到char [] Objective-c
- 25. 寫NSString到文件
- 26. 轉換:NSString到NSDate
- 27. 轉換NSDate到NSString
- 28. iPhone SDK NSString到NSDate
- 29. 傳遞NSString到Webview
- 30. NSData到NSString編碼
要請求支持從NSString中讀取未簽名的值,請訪問http://bugreport.apple.com並提交一份有關雷達:// 2264733對'component | X'。 – 2013-01-23 21:46:42