저는 매우 기본적인 앱을 만들려고합니다. 앱의 한 부분은 4 개의 textField와이 textField의 합을 계산하는 버튼이 있다는 것입니다. 내가 직면하고있는 문제는 첫 번째 textField에 값 10을 입력 한 다음 버튼을 누릅니다. 결과는 10이 될 것입니다. 그러나 다른 텍스트 필드에 아무 것도 입력하지 않고 다시 누르면 20이됩니다! 또한, 다른 textField 중 하나에 20을 입력하면 결과는 40입니다 !! 결과는 30이 아니어야합니다 40! 버튼을 누를 때 모든 textFields에 0을 할당한다고 생각한 가능한 옵션 (가능한 한 아직 시도하지 않음). 하지만 앱을 더 똑똑하게 만들고 결과를 추적 할 수 있기를 바랍니다.스위프트 3에서 이미 계산 된 UITextField 값을 무시하는 방법은 무엇입니까?
@IBAction func calBtnPressed(_ sender: UIButton) {
var benifit:[Double] = []
var textFields: [Double] = []
if initialBalance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = initialBalance.text {
// these lines of code will convert arabic numbers to English ones in case the user uses Arabic number
let initialStr: String = temp
let initialFormatter: NumberFormatter = NumberFormatter()
initialFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let initialFinal = initialFormatter.number(from: initialStr)
benifit.append(Double(initialFinal!))
}
}
if income.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = income.text {
// these lines of code will convert Arabic numbers to English ones in case the user uses Arabic number
let incomeStr: String = temp
let incomeFormatter: NumberFormatter = NumberFormatter()
incomeFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let incomeFinal = incomeFormatter.number(from: incomeStr)
benifit.append(Double(incomeFinal!))
}
}
if salaries.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = salaries.text {
let salariesStr: String = temp
let salariesFormatter: NumberFormatter = NumberFormatter()
salariesFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let salariesFinal = salariesFormatter.number(from: salariesStr)
textFields.append(Double(salariesFinal!))
}
}
if tools.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = tools.text {
let toolsStr: String = temp
let toolsFormatter: NumberFormatter = NumberFormatter()
toolsFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let toolsFinal = toolsFormatter.number(from: toolsStr)
textFields.append(Double(toolsFinal!))
}
}
if maintinance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = maintinance.text {
let maintinanceStr: String = temp
let maintinanceFormatter: NumberFormatter = NumberFormatter()
maintinanceFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let maintinanceFinal = maintinanceFormatter.number(from: maintinanceStr)
textFields.append(Double(maintinanceFinal!))
}
}
if other.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = other.text {
let otherStr: String = temp
let otherFormatter: NumberFormatter = NumberFormatter()
otherFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let otherFinal = otherFormatter.number(from: otherStr)
textFields.append(Double(otherFinal!))
}
}
for textField in textFields {
sumExpenses += textField
}
for ben in benifit{
sumBenifit += ben
}
totalExpenses.text = String(sumExpenses)
totalAfterSubtractingExpenses.text = String(sumBenifit - sumExpenses)
sumBenifit -= sumExpenses
}
문제와 관련이 없지만이 코드에는 배열을 사용해야합니다. 서로 다른 버튼에 대해서만 동일한 다섯 줄을 여러 번 반복하면 코드를 읽기 어렵게하고 간결하게 만듭니다. –
사실, 당신은 이미 textFields의 배열을 가지고 있습니다. 왜 그렇게 사용하지 않았습니까? –
의견을 보내 주셔서 감사합니다.하지만 이해할 수 있는지 잘 모르겠습니다. 어느 5 줄을 반복할까요? 아라비아 숫자를 영어로 변환하는 것은 5 줄입니까? 나는 정말로 내 코드를 더 좋아하기를 정말 좋아한다. – user2066392