我正在解决正确计算的问题(这太棒了)但是如果我在模拟器中留下一个空白的文本字段就会崩溃(不是真棒)或者:当编码方式不同时,空白文本字段没有崩溃(不好)但计算不正确(不好)。
这是我正在尝试做的事情: 获取4个值的用户输入:计数纤维,计算空白纤维(如果它为零则确定),计算的字段和样品体积。我提出了默认值,希望能够处理文本框为空白的任何实例,希望能够防止崩溃。然后应用程序计算纤维密度,检测限和样品结果。现在非常简单,但我希望稍后加入。
我的主要问题是选项。我已经阅读过并尽力准确地编写代码,但有时候Xcode迫使我进入角落。所以我的应用程序在文本字段为空时崩溃。或者我可以防止崩溃,但不知何故,默认值将在计算中使用,实际数据将关闭。
我必须错误地使用if-let,所以我尝试了你在下面的代码中看到的内容。
我会把代码放在下面,并且一如既往地感谢任何帮助,并理解我是新手并且在自学成才的同时试图理解这些概念(除了这里学到的经验教训)。谢谢!
import UIKit
类ViewController:UIViewController {
@IBOutlet weak var fibersCountedTextField: UITextField!
@IBOutlet weak var blankFibersCountedTextField: UITextField!
@IBOutlet weak var fieldsCountedTextField: UITextField!
@IBOutlet weak var sampleVolumeTextField: UITextField!
@IBOutlet weak var fiberDensityLabel: UILabel!
@IBOutlet weak var limitOfDetectionLabel: UILabel!
@IBOutlet weak var sampleResultLabel: UILabel!
@IBAction func calculateBtnPressed(_ sender: Any) {
var blankCheck: String?
var blankFibersCounted: Double?
var fibersCheck: String?
var fibersCounted: Double?
var fieldsCheck: String?
var fieldsCounted: Double?
var volumeCheck: String?
var sampleVolume: Double?
var sampleResultNumerator: Double?
var sampleResultDenominator: Double?
var sampleResult: Double?
var sampleResultRounded: Double
var fiberDensity: Double
var fiberDensityRounded: Double
var limitOfDetectionNumerator: Double
var limitOfDetectionDenominator: Double
var sampleLimitOfDetection: Double
var sampleLimitOfDetectionRounded: Double
struct TypicalSampleData {
let typicalBlankFibersCounted: Double
let typicalFibersCounted: Double
let typicalFieldsCounted: Double
let typicalSampleVolume: Double
init(typicalBlankFibersCounted: Double = 4.0, typicalFibersCounted: Double = 5.5, typicalFieldsCounted: Double = 100.0, typicalSampleVolume: Double = 555.0) {
self.typicalBlankFibersCounted = typicalBlankFibersCounted
self.typicalFibersCounted = typicalFibersCounted
self.typicalFieldsCounted = typicalFieldsCounted
self.typicalSampleVolume = typicalSampleVolume
}
}
//this code calculates correctly but crashes if text boxes left empty in the simulator.
let defaultSampleData = TypicalSampleData()
//if let blankCheck = blankFibersCountedTextField.text {
// blankFibersCounted = Double(blankCheck)
//} else {
// blankFibersCounted = defaultSampleData.typicalBlankFibersCounted
//}
//blankCheck = blankFibersCountedTextField.text
//var blankCheckDbl: Double?
//if let blankCheck = blankFibersCountedTextField.text {
// blankFibersCounted = Double(blankCheck)
//}; blankFibersCounted = defaultSampleData.typicalBlankFibersCounted
//if let fibersCheck = fibersCountedTextField.text {
// fibersCounted = Double(fibersCheck)
//} else {
// fibersCounted = defaultSampleData.typicalFibersCounted
//}
if blankFibersCountedTextField != nil {
blankCheck = blankFibersCountedTextField.text
} else {
blankFibersCounted = defaultSampleData.typicalBlankFibersCounted
}
if blankCheck == blankFibersCountedTextField.text {
blankFibersCounted = Double(blankCheck!)
} else {
blankFibersCounted = defaultSampleData.typicalBlankFibersCounted
}
if fibersCountedTextField != nil {
fibersCheck = fibersCountedTextField.text
} else {
fibersCounted = defaultSampleData.typicalFibersCounted
}
if fibersCheck == fibersCountedTextField.text {
fibersCounted = Double(fibersCheck!)
} else {
fibersCounted = defaultSampleData.typicalFibersCounted
}
if fieldsCountedTextField != nil {
fieldsCheck = fieldsCountedTextField.text
} else {
fieldsCounted = defaultSampleData.typicalFieldsCounted
}
if fieldsCheck == fieldsCountedTextField.text {
fieldsCounted = Double(fieldsCheck!)
} else {
fieldsCounted = defaultSampleData.typicalFieldsCounted
}
if sampleVolumeTextField != nil {
volumeCheck = sampleVolumeTextField.text
} else {
sampleVolume = defaultSampleData.typicalSampleVolume
}
if volumeCheck == sampleVolumeTextField.text {
sampleVolume = Double(volumeCheck!)
} else {
sampleVolume = defaultSampleData.typicalSampleVolume
}
//if let fibersCheck = fibersCountedTextField.text {
// fibersCounted = Double(fibersCheck)
//}; fibersCounted = defaultSampleData.typicalFibersCounted
//if let fieldsCheck = fieldsCountedTextField.text {
// fieldsCounted = Double(fieldsCheck)
//} else {
// fieldsCounted = defaultSampleData.typicalFieldsCounted
//}
//if let fieldsCheck = fieldsCountedTextField.text {
// fieldsCounted = Double(fieldsCheck)
//}; fieldsCounted = defaultSampleData.typicalFieldsCounted
//if let volumeCheck = sampleVolumeTextField.text {
// sampleVolume = Double(volumeCheck)
//} else {
// sampleVolume = defaultSampleData.typicalSampleVolume
//}
//if let volumeCheck = sampleVolumeTextField.text {
// sampleVolume = Double(volumeCheck)
//}; sampleVolume = defaultSampleData.typicalSampleVolume
//calculate result
if let fibersCounted = fibersCounted, fibersCounted < 5.5 {
sampleResultNumerator = (385 * (5.5 / 100))
sampleResultDenominator = (7.85 * sampleVolume!)
sampleResult = sampleResultNumerator! / sampleResultDenominator!
} else {
sampleResultNumerator = 385 * ((fibersCounted! - blankFibersCounted!) / fieldsCounted!)
sampleResultDenominator = (7.85 * sampleVolume!)
sampleResult = sampleResultNumerator! / sampleResultDenominator!
}
sampleResultRounded = Double(round(1000 * sampleResult!)/1000)
//calculate sample limit of detection
//insert if-let statement here, similar to above?
limitOfDetectionNumerator = 385 * (5.5 / 100)
limitOfDetectionDenominator = (7.85 * sampleVolume!)
sampleLimitOfDetection = limitOfDetectionNumerator / limitOfDetectionDenominator
sampleLimitOfDetectionRounded = Double(round(1000 * sampleLimitOfDetection)/1000)
//calculate fiber density
//insert if-let statement here, similar to above?
fiberDensity = ((fibersCounted! - blankFibersCounted!) / fieldsCounted!) / 0.00785
fiberDensityRounded = Double(round(1000 * fiberDensity)/1000)
fiberDensityLabel.text = String(fiberDensityRounded)
limitOfDetectionLabel.text = String(sampleLimitOfDetectionRounded)
sampleResultLabel.text = String(sampleResultRounded)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:0)
有多种方法可以处理您的任务,但这是一个非常直接的方法(用您的实际@IBOutlet名称替换fieldA
,fieldB
等):
@IBAction func calculateBtnPressed(_ sender: Any) {
let defaultSampleData = TypicalSampleData()
// get the text from fieldA - set it to "" if fieldA.text is nil
let textA = fieldA.text ?? ""
// get the text from fieldB - set it to "" if fieldB.text is nil
let textB = fieldB.text ?? ""
// if textA CAN be converted to a Double, use it, otherwise use default value
let valA = Double(textA) ?? defaultSampleData.typicalBlankFibersCounted
// if textB CAN be converted to a Double, use it, otherwise use default value
let valB = Double(textB) ?? defaultSampleData.typicalFibersCounted
// set resultLabel.text to the product of valA and valB
resultLabel.text = "Result: \(valA * valB)"
}
这个想法是:确保你的“等式”的每个部分都有默认值。
当然,根据您的实际用例,您可能希望显示错误弹出消息而不是使用默认值。但这应该让你顺利。