在Swift中,可以使用Int()函数将字符串转换为NSInteger类型。示例如下:

let string = "123"
if let number = Int(string) {
    let nsNumber: NSInteger = NSInteger(number)
    print(nsNumber)
} else {
    print("无法将字符串转换为整数")
}

首先,使用Int()函数将字符串转换为Int类型。然后,使用NSInteger()Int类型转换为NSInteger类型。如果字符串无法转换为整数,则Int()函数返回nil。因此,在转换之前,最好使用可选绑定(if let)来检查转换是否成功。

String 转 NSInteger swift

原文地址: http://www.cveoy.top/t/topic/iY9Z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录