xcode 如何使用 Swift 将 NSString 转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26662471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert an NSString to an integer using Swift?
提问by rocket101
I need to convert an NSString
to an integer in Swift: Here's the current code I'm using; it doesn't work:
我需要NSString
在 Swift 中将 an转换为整数:这是我正在使用的当前代码;它不起作用:
var variable = (NSString(data:data, encoding:NSUTF8StringEncoding))
exampeStruct.otherVariable = (variable).intValue
Variable is a normal varable, and exampleStruct is a struct elsewhere in the code with a subvariable otherVariable.
Variable 是普通变量,exampleStruct 是代码中其他地方的结构体,带有子变量 otherVariable。
I expect it to set exampleStruct.otherVariable
to an int
value of the NSString
, but I get the following error:
我希望它设置exampleStruct.otherVariable
为 的int
值NSString
,但出现以下错误:
"Cannot convert the expression's type () to type Float"
How do I convert an NSString
to int
in Swift?
如何在 Swift 中转换NSString
为int
?
采纳答案by Leo Dabus
You have to unwrap 2 optionals there, notice that I have also declared variable as String adding ":String" there. Example:
您必须在那里解开 2 个选项,请注意,我还在那里将变量声明为字符串,并在那里添加了 ":String"。例子:
if let variable:String = NSString(CString:"1", encoding: NSUTF8StringEncoding) {
if let otherVariable = variable.toInt() {
println(otherVariable) // "1"
}
}
In your case you should do like this:
在您的情况下,您应该这样做:
if let variable:String = NSString(data: data, encoding: NSUTF8StringEncoding) {
if let exampeStruct.otherVariable = variable.toInt() {
println(exampeStruct.otherVariable) // "1"
}
}
回答by MahatmaManic
It looks to me like the problem might not be the Int conversion, but rather that the exampleStruct is expecting a Float.
在我看来,问题可能不是 Int 转换,而是 exampleStruct 需要一个 Float。
If that's not the issue however (and granted, Xcode errors for Swift often seem to be more about the line number rather than about the actual problem) then something like this should work for you?
然而,如果这不是问题(当然,Swift 的 Xcode 错误通常似乎更多地与行号有关,而不是与实际问题有关),那么这样的事情应该适合您吗?
var ns:NSString = "1234"
if let i = (ns as String).toInt() {
exampleStruct.otherVariable = i
}
回答by surui
I know you already got your answer, but I just want to explain what (I think) might not be trivial
我知道你已经得到了答案,但我只是想解释一下(我认为)可能不是微不足道的
First, we have some NSData
we want to convert to NSString
, because no one guaranties the data is a valid UTF8 buffer, it return an optional
首先,我们有一些NSData
我们想转换为NSString
,因为没有人保证数据是有效的 UTF8 缓冲区,它返回一个可选的
var variable = NSString(data:data, encoding:NSUTF8StringEncoding)
Which means variable: NSString?
意思是 variable: NSString?
Usually NSString
is bridged to swift's String
, but in this case, we use an NSString
constructor - you can think about it more as a "Foundation"-like syntax that wasn't directly imported to swift (as there's no bridge for NSData
)
通常NSString
桥接到 swift 的String
,但在这种情况下,我们使用NSString
构造函数 - 您可以将它更多地视为一种“基础”式的语法,它没有直接导入到 swift (因为没有桥接NSData
)
we can still use the 'Foundation' way with NSString
我们仍然可以使用“基础”方式 NSString
if let unwrappedVariable = variable {
var number = unwrappedVariable.intValue
}
if number
is a Float
, but the string is a string representation of an integer
ifnumber
是 a Float
,但字符串是整数的字符串表示
if let unwrappedVariable = variable {
var number: Float = Float(unwrappedVariable.intValue)
}
if both number
and the string (representation of) are floats:
如果number
和 字符串(的表示)都是浮点数:
if let unwrappedVariable = variable {
var number:Float = unwrappedVariable.floatValue
}
Anyway, there's a small problem with using Foundation. For these types of conversions it has no concept of an optional value (for int, float). It will return 0 if it cannot parse a string as and integer or float. That's why it's better to use swift native String
:
无论如何,使用Foundation有一个小问题。对于这些类型的转换,它没有可选值的概念(对于 int、float)。如果它不能将字符串解析为整数或浮点数,它将返回 0。这就是为什么最好使用 swift native 的原因String
:
if let variable: String = NSString(data: data, encoding: NSUTF8StringEncoding) {
if let integer = variable.toInt() {
var integerNumber = integer
var floatNumber = Float(integer)
}
}