Ios 缩短swift中if语句的长度
我有一个有3个屏幕的应用程序,屏幕1(类别)有许多按钮,屏幕2(详细信息)有更多按钮,屏幕3根据前两个屏幕中按下的按钮显示文本 屏幕1上的按钮保持不变,屏幕2上的按钮根据在第一个屏幕上按下的按钮的不同而有所不同。为此,我现在有一个很长的if语句,我正试图找到减少它的方法 我能帮上忙,但我觉得我遗漏了一些简单的东西 如果语句与此类似:Ios 缩短swift中if语句的长度,ios,swift,if-statement,swift3,conditional-statements,Ios,Swift,If Statement,Swift3,Conditional Statements,我有一个有3个屏幕的应用程序,屏幕1(类别)有许多按钮,屏幕2(详细信息)有更多按钮,屏幕3根据前两个屏幕中按下的按钮显示文本 屏幕1上的按钮保持不变,屏幕2上的按钮根据在第一个屏幕上按下的按钮的不同而有所不同。为此,我现在有一个很长的if语句,我正试图找到减少它的方法 我能帮上忙,但我觉得我遗漏了一些简单的东西 如果语句与此类似: if selectedCategory == "Option 1" { if selectedDetail == "Detail A" {
if selectedCategory == "Option 1" {
if selectedDetail == "Detail A" {
selectedURL = optionOneData.detailA
} else if selectedDetail == "Detail C" {
selectedURL = optionOneData.detailC
} else if selectedDetail == "Detail E" {
selectedURL = optionOneData.detailE
} else if selectedDetail == "Detail G" {
selectedURL = optionOneData.detailG
} else if selectedDetail == "Detail J" {
selectedURL = optionOneData.detailJ
} else {
print("Invalid selection, something went wrong.")
}
} else if selectedCategory == "Option 2" {
if selectedDetail == "Detail B" {
selectedURL = optionTwoData.detailB
} else if selectedDetail == "Detail C" {
selectedURL = optionTwoData.detailC
} else if selectedDetail == "Detail D" {
selectedURL = optionTwoData.detailD
} else if selectedDetail == "Detail E" {
selectedURL = optionTwoData.detailE
} else if selectedDetail == "Detail F" {
selectedURL = optionTwoData.detail F
} else if selectedDetail == "Detail G" {
selectedURL = optionTwoData.detailG
} else if selectedDetail == "Detail H" {
selectedURL = optionTwoData.detailH
} else if selectedDetail == "Detail I" {
selectedURL = optionTwoData.detailI
} else if selectedDetail == "Detail J" {
selectedURL = optionTwoData.detailJ
} else {
print("Invalid selection, something went wrong.")
}
不是我的实际代码,为上下文重命名了变量
这只代表了我现有代码的1/3,因此我希望减少代码量,以提高效率和可读性
谢谢。从您的代码中可以看出,一个
Switch()
语句最适合您,而不是使用这个长的if-else
语句。这是这种情况下的最佳做法。您可以使用switch()
语句:
var selectedCategory = ""
switch selectedCategory {
case "Option 1" :
var selectedDetail = ""
switch selectedDetail {
case "Detail A"
selectedURL = optionOneData.detailA
case "Detail C"
selectedURL = optionOneData.detailC
....
default:
print("Invalid selection, something went wrong.")
}
case "Option 2":
var selectedDetail = ""
switch selectedDetail {
case "Detail A"
selectedURL = optionOneData.detailA
case "Detail C"
selectedURL = optionOneData.detailC
....
default:
print("Invalid selection, something went wrong.")
}
default:
print("Invalid selection, something went wrong.")
}
在字典中保留所有选择细节及其url,如下所示
let selectionInfo = ["Detail A": optionOneData.detailA, "Detail E": optionOneData.detailE]
并使用此选项获取选定的url
selectedURL = selectionInfo[selectedDetail]
如果selectedURL为nil,则没有有效的选择。
Switch()
语句替换longif elseif else
条件语句。尝试使用它。同时创建您的选项,因为Enum
将使您的代码更具可读性,但您仍然必须使用switch
分配数据和字符串switch语句仍然很长,您的数据可能很容易保存在字典或数组中。selectedCategory==“选项1”
条件没有任何意义,因为您可以在不检查的情况下生成所有if/else
语句。注意,您可以在元组上使用单个开关,例如开关(selectedCategory,selectedDetail){case(“Option2”,“DetailA”):…
最后选择了Switch,如果我想在按下这些按钮时运行其他东西,它的可读性会更好。这可能是(我认为)最好的方法。谢谢!