小编365*_*ses的帖子

Swift从String中提取Int,Float或Double值(类型转换)

你能帮帮我吗?我需要了解如何将String转换为Int,Float或Double!当我试图从UITextField获取值并需要这种类型的转换时,会发生此问题!

我过去常常这样做:

var myValue : Float = myTextField.text.bridgeToObjectiveC().floatValue
Run Code Online (Sandbox Code Playgroud)

但是从Xcode 6 beta 6开始它似乎不再起作用了!

我也试过这样:

var str = "3.14"

// Conversion from StringValue to an Int
var intValue : Int = str.toInt()!

// Other converstion from StringValue to an Int
var intOtherValue : Int = Int(str)

// Converstion from StringValue to a Float
var floatValue : Float = str.bridgeToObjectiveC().floatValue

// Converstion from StringValue to a Double
var doubleValue : Double = Double(str)
Run Code Online (Sandbox Code Playgroud)

请帮帮我或告诉我在哪里可以找到答案!非常感谢!

string floating-point double int swift

9
推荐指数
1
解决办法
3万
查看次数

标签 统计

double ×1

floating-point ×1

int ×1

string ×1

swift ×1