我正在尝试制作一个使用3个变量的代码(在Swift 4 xcode 9中),其中2个变量由用户通过TextFields给出。另一个变量的值将根据哪个变量留空来计算。例如,如果用户将textfield1留空,则当按下按钮时,textfield2和textfield3的输入将用于计算。如果用户将textfield2留空,则使用textfield1和textfield3的输入,以此类推...我还设置了委托,将UITextField转换为float等。
我正在尝试创建一个选择器,在该选择器的基础上textfield留空,但没有成功...
提前道谢!
//instance variables to hold values entered by the user
var one : Float = 0
var two : Float = 0
var three : Float = 0
//TextFields variables
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
@IBOutlet weak var textField3: UITextField!
func calculate() {
var result : Float = 0
// *** Selector ***
//if user leaves textfield1 blank
if (textField1.text?.isEqual(nil))! {
result = textField2 * textfield3
resultLabel.text = "\(result)"
}
//if user leaves textField2 blank
else if (textField2.text?.isEqual(nil))! {
result = textfield1 / textField3
resultLabel.text = "\(result)"
}
//if user leaves textField3 blank
else if (textfield3.text?.isEqual(nil))! {
result = textfield1 + textfield2
resultLabel.text = "\(result)"
}
}
您试图对uitextfield
执行数学运算,这显然是不可能的。文本字段包含字符串。您希望对这些字符串表示的float
进行数学运算。
如果您将您的问题概括一下,它基本上可以归结为:
float
Sfloat
执行数学运算,并将结果输出到标签代码:
func calculate() {
// These are the possible operations that you will perform
// Each is a closure that takes 2 Floats and return a Float
let plus = { (lhs: Float, rhs: Float) in lhs + rhs }
let multiply = { (lhs: Float, rhs: Float) in lhs * rhs }
let divide = { (lhs: Float, rhs: Float) in lhs / rhs }
var lhsStr = "" // the string stored in the first non-empty text field
var rhsStr = "" // the string stored in the second non-empty text field
var operation = plus // don't worry about the default. We will change this later
// Find which field is empty
// We allow only one of the fields to be empty, not 0, 2, or 3.
switch (textField1.text!.isEmpty, textField2.text!.isEmpty, textField3.text!.isEmpty) {
case (true, false, false):
lhsStr = textField2.text!
rhsStr = textField3.text!
operation = multiply
case (false, true, false):
lhsStr = textField1.text!
rhsStr = textField3.text!
operation = divide
case (false, false, true):
lhsStr = textField1.text!
rhsStr = textField2.text!
operation = plus
default:
fatalError("One and only one of the fields must be empty")
}
// Now convert the 2 Strings to Floats
if let lhs = Float(lhsStr), let rhs = Float(rhsStr) {
let result = operation(lhs, rhs)
resultLabel.text = "\(result)"
} else {
fatalError("Cannot convert string to number")
}
}
你正在尝试做的是相当手动的,不是推荐的方式。Swift支持很多转换函数来完成这样的工作。
首先创建一个数组来存储文本字段:
var inputs: [UITextField] = [UITextField]()
override func viewDidLoad() {
super.viewDidLoad()
inputs.append(textField1)
inputs.append(textField2)
inputs.append(textField3)
}
现在您有了一个包含所有输入的数组。我们现在的工作是转换输入并得到结果值:
func calculation() {
var blankIndex = 0 // keep track of which input is blank
// let transform our inputs into float values
let values = inputs.enumerated()
.map { (i, textField) -> Float? in
if let text = textField.text, !text.isEmpty {
blankIndex = i // we know which input is blank
return Float(text)!
}
return nil
}
.filter { $0 != nil }
.map { $0! }
// now we need to validate out inputs
// only one input is left blank by the user
if values.count != 2 {
// show alert for notifying user about this
// stop our calculation
return
}
// now our values contain only valid inputs the we need,
// we can start do our calculation now
let result: Float
switch blankIndex {
case 0: result = values[0] * values[1]
case 1: result = values[0] / values[1]
default: result = values[0] + values[1]
}
}
此时,result变量是您需要的结果
这里是SQL新手。我正在尝试生成一个成本计算查询,该查询输出员工工时卡信息,并根据有效的员工成本计算率计算成本。 我的问题与这里的问题类似:追溯有效生效日期与重叠日期更改,但我不处理追溯activity或重叠日期范围。 表示例(rate表中的null值表示当前费率): 查询(返回由多个速率条目(显然)引起的dupes): 所需输出: 我在子查询中使用运算符来隔离基于计费日期的正确费率,但没有任何运
问题内容: 我有一个column ,一列和作为实例化路径的一列。 看起来像 我需要根据此表进行一些查询。 我需要做的查询是 选择所有9个孩子 可以正常工作,直到您将ID替换为1或19,因为开头没有ID 。 将选择数字以1结尾的所有行,因此1、11、21、31、211等 将在第1行或第19行中正常工作 所以测试员; 我能提出最好的建议吗? 选择9,但没有子儿的直接孩子 对于这个测试仪; 会很好的工作
我想计算选择属性中的选项,但我的测试失败了,这是我的规范: 它给了我错误: C:\wamp\www\First-angular-App
问题内容: 我有一张叫的桌子。在该表中,我有以下栏目:,,, 我有另一个表,唯一重要的列是,我想将其与表行进行比较。 该表如下所示: 假设我有一个表(user_meta)像这样: 我想为每个检索单个行,但前提是公司ID和user_type正确。 我想检索在查询中发送的具有相同companyID的所有用户,因此,假设$ companyID = 2,然后所有具有=’staff’的用户。 因此,user
问题内容: 我正在尝试使用ST_SnapToGrid,然后使用网格单元格(x,y)。这是我首先要做的: 我不想为和重新计算。所以我将其更改为使用子查询: 但是,当我运行时,这两个查询都具有完全相同的执行计划: 问题 :PostgreSQL会重用的结果值吗? 如果没有,是否有办法做到这一点? 问题答案: 测试时间 您不会在输出中看到每行各个功能的评估。 测试以获取实际查询时间以比较总体效果。运行几次