Я делаю приложение калькулятора для химических дозировок.Есть три текстовых поля, объем, текущий и целевой.для объема пользователь может выбрать между галлоном и литром.В коде я сделал оператор переключения для расчета для каждого продукта, который находится в переменной result
.Оттуда у меня есть функция configureLabel, которая просто покрывает result
в мл или граммах.Однако, если пользователь выбирает литры из сегментированного элемента управления, мне нужно разделить result
на 3,79.
Как / Где мне это сделать?Должен ли я сделать это в операторе switch для моих расчетов?Или где этикетка?
Вот весь соответствующий код.
func configureLabel(for result: Double) {
// Liquid
let inML = (result/10)
let inFlOz = ((result * 0.033814)/10)
let inTsp = ((result/5)/10)
// Powder
let grams = (result / 10)
let tsp = (result/4.6)/10
let oz = (result/28.35)/10
// Rounds to 2 decimals, You can use rounded() but there will be no decimals in place.
let roundedMl = Double(round(1000 * inML) / 1000)
let roundedFlOz = Double(round(1000 * inFlOz) / 1000)
let roundedtsp = Double(round(1000 * inTsp) / 1000)
let roundedGrams = Double(round(1000 * grams) / 1000)
let roundedPowderTsp = Double(round(1000 * tsp) / 1000)
let roundedOz = Double(round(1000 * oz) / 1000)
/*
If in gallon leave the label as is, if it is in liters divide each result by 3.79
*/
if chosenProduct.productType == liquid {
mlGrLabel.text = String(roundedMl)
ozLabel.text = String(roundedFlOz)
tspLabel.text = String(roundedtsp)
} else if chosenProduct.productType == powder {
mlGrLabel.text = String(roundedGrams)
ozLabel.text = String(roundedOz)
tspLabel.text = String(roundedPowderTsp)
}
}
func configureProducts() {
let aVolume = Double(waterVolumeTextField.text!)
let aCurrent = Double(currentLevelTextField.text!)
let aTarget = Double(targetLevelTextField.text!)
let ratio = chosenProduct.productRatio
guard let volume = aVolume, let current = aCurrent, let target = aTarget else { return }
self.resignFirstResponder()
switch chosenProduct.name {
case aquaForestKHPlus.name, brightwellAlk.name, brightwellReefCodeB.name, brightwellNanoCodeB.name, redseaReefFoundationB.name:
let result = calculateLiquidAlk(with: ratio, from: current, to: target, volume: volume)
configureLabel(for: result)
case seachemReefCarb.name:
let result = 10 * (target - current) * volume
configureLabel(for: result)
case seachemReefFusion2.name:
let result = (10 / ratio) * (target - current) * volume
configureLabel(for: result)
case aquaForestKHBuffer.name:
let result = 10 * ((target - current) / 1) * ratio * volume
configureLabel(for: result)
case brightwellAlkP.name, brightwellReefCodeBP.name, redseaReefFoundationBP.name:
let result = calculatePowderAlk(with: ratio, from: current, to: target, volume: volume)
configureLabel(for: result)
case seachemReefBuilder.name, seachemReefBuffer.name:
let result = 10 * (target - current) * ratio * volume
configureLabel(for: result)
// CALCIUM
case aquaForestCAPlus.name, redseaReefFoundationA.name:
let result = calculateCalLiq(with: ratio, from: current, to: target, volume: volume)
configureLabel(for: result)
case aquaForestCAP.name, bwCalcionPow.name, bwReefCodeAPow.name, fpCalChlo.name, tlfCa.name, esvbc2.name, kentTurbo.name, redseaReefFoundationAPow.name:
let result = calculateLiquidAlk(with: ratio, from: current, to: target, volume: volume)
configureLabel(for: result)
case bwNanoCodeA.name, kentTech.name, bwReefCodeALiq.name, bwCalcionLiq.name, seachemReefFusion1L.name:
let result = calculateCalcium(with: ratio, from: current, to: target, volume: volume)
configureLabel(for: result)
case seachemReefAdv.name, seachemReefComp.name:
let result = calculateSeaChemCal(with: ratio, from: current, to: target, volume: volume)
configureLabel(for: result)
// Magnesium
case aquaforestMgP.name, bwMgLiq.name, bwMgP.name, kentTechM.name, esvmg.name, rsmgpw.name, rsmgpw.name:
let result = calculateMagnesium(with: ratio, from: current, to: target, volume: volume)
configureLabel(for: result)
// Phosphate
case brightwellPhosphate.name, redseaNoPox.name, aquaforestPhosphateMinus.name:
hideFields()
let result = ratio * volume
configureLabel(for: result)
// Trace element
case afFluorine.name, afIodium.name, afIron.name, afKilum.name, afMicroE.name, afStrontium.name:
let result = ratio * volume
configureLabel(for: result)
default:
break
}
}