swift - 初始化浮点数 属性 到 UITextField 输入

swift - Initialising a float property to the UITextField input

我正在尝试在 UITextField 中从用户键入的文本字段初始化一个 属性 类型的浮点数。

我收到以下代码的 UITextField is not convertible to 'float' 错误。

float变量和textfield类型的代码

    var forceVar : Float = 0.0
    @IBOutlet weak var forceEntered: UITextField!

从 UITextField

初始化 属性
forceVar = forceEntered

您只需将文本转换为 NSString,然后提取 floatValue:

forceVar = (forceEntered.text as NSString).floatValue