将字符串转换为浮动在苹果的Swift中 [英] Convert String to float in Apple's Swift

查看:155
本文介绍了将字符串转换为浮动在苹果的Swift中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图转换从UITextField取得的数字,我认为它实际上是字符串,并将它们转换为浮点数,所以我可以将它们相乘。

我有两个 UITextfield ,声明如下:

  @IBOutlet var wage:UITextField 
@IBOutlet var hour:UITextField

当用户按下UIButton I想要计算用户赚取的工资,但是我不能,因为我需要先将它们转换成浮动状态,然后才能使用它们。



我知道如何将它们转换为一个整数:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ()!

但是,我不知道如何将它们转换为浮动。


Swift 2.0

现在使用 Swift 2.0 ,您可以使用 Swift 2.0 Float(Wage.text)返回一个 Float?类型。比下面的解决方案更清楚,它返回 0



如果你想要一个 0 为无效 Float 的值可以使用 Float(Wage.text)? 0 如果它不是有效的 Float ,它将返回 0




$ b

旧解决方案



处理这个问题的最好方法是直接投射: / p>

  var WageConversion =(Wage.text as NSString).floatValue 

我实际上创建了一个扩展来更好地使用它:

 扩展字符串{
var floatValue:Float {
return(self as NSString).floatValue
}
}

现在您可以调用 var WageConversion = Wage.text.floatValue 允许扩展为你处理桥梁!

这是一个很好的实现,因为它可以处理实际的浮动(输入

code>),并且还有助于防止用户将文本复制到输入字段( 12p.34 ,甚至 12.12.41 )。



显然,如果Apple确实添加了一个 floatValue 到Swift这将抛出一个异常,但它可能是好的同时。如果他们稍后添加它,那么所有你需要做的修改你的代码是删除扩展,一切都将无缝工作,因为你已经调用 .floatValue



另外,变量和常量应该以小写开头(包括 IBOutlets


I'm trying to convert numbers taken from a UITextField, which I presume, are actually strings, and convert them to float, so I can multiply them.

I have two UITextfields which are declared as follows:

@IBOutlet var wage: UITextField
@IBOutlet var hour: UITextField

When the user presses a UIButton I want to calculate the wages the user earns, but I can't, as I need to convert them to floats first, before I can use them.

I know how to convert them to an integer by doing this:

var wageConversion:Int = 0
wageConversion = wage.text.toInt()!

However, I have no idea how to convert them to floats.

解决方案

Swift 2.0+

Now with Swift 2.0 you can just use Float(Wage.text) which returns a Float? type. More clear than the below solution which just returns 0.

If you want a 0 value for an invalid Float for some reason you can use Float(Wage.text) ?? 0 which will return 0 if it is not a valid Float.


Old Solution

The best way to handle this is direct casting:

var WageConversion = (Wage.text as NSString).floatValue

I actually created an extension to better use this too:

extension String {
    var floatValue: Float {
        return (self as NSString).floatValue
    }
}

Now you can just call var WageConversion = Wage.text.floatValue and allow the extension to handle the bridge for you!

This is a good implementation since it can handle actual floats (input with .) and will also help prevent the user from copying text into your input field (12p.34, or even 12.12.41).

Obviously, if Apple does add a floatValue to Swift this will throw an exception, but it may be nice in the mean time. If they do add it later, then all you need to do to modify your code is remove the extension and everything will work seamlessly, since you will already be calling .floatValue!

Also, variables and constants should start with a lower case (including IBOutlets)

这篇关于将字符串转换为浮动在苹果的Swift中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆