我有一個接受Textfield
作爲參數並基於標記的函數,數字發生變化;以下是代碼示例減少多個if else語句的環化複雜度
func textFieldDidChange(_ textField: UITextField) {
if(textField.tag == 0){
measureMentObject?.bloodPressureSystolic = myNumber
}else if(textField.tag == 1){
measureMentObject?.bloodPressureDiastolic = myNumber
}else if(textField.tag == 2){
measureMentObject?.heartRate = myNumber
}else if(textField.tag == 3){
measureMentObject?.bodyTemperature = myNumber
}else if(textField.tag == 4){
measureMentObject?.respitoryRate = myNumber
}else if(textField.tag == 5){
measureMentObject?.o2Saturation = myNumber
}else if(textField.tag == 6){
measureMentObject?.painScale = myNumber
}else if(textField.tag == 7){
measureMentObject?.weight = myNumber
}else if(textField.tag == 8){
measureMentObject?.po = myNumber
}else if(textField.tag == 13){
measureMentObject?.gastricTube = myNumber
}
}
在這種情況下,如何降低環化複雜性?
您可以使用開關 –
使用標籤創建枚舉並使用開關。以確定標籤:) –
檢查我的答案我已爲您添加示例 –