Swift: String to NSInteger Conversion
In Swift, you can convert a String to an NSInteger using the Int() function. Here's an example:
let string = '123'
if let number = Int(string) {
let nsNumber: NSInteger = NSInteger(number)
print(nsNumber)
} else {
print('无法将字符串转换为整数')
}
First, use the Int() function to convert the String to an Int type. Then, use NSInteger() to convert the Int type to an NSInteger type.
If the String cannot be converted to an integer, the Int() function returns nil. Therefore, it's best to use optional binding (if let) to check if the conversion was successful before proceeding.
原文地址: https://www.cveoy.top/t/topic/qFb1 著作权归作者所有。请勿转载和采集!