在swift中减少if语句的长度

时间:2017-02-16 09:26:24

标签: ios swift if-statement swift3 conditional-statements

我有3个屏幕的应用程序,屏幕1(类别)有多个按钮,屏幕2(细节)有更多按钮,屏幕3显示基于前2个屏幕中按下了什么按钮的文本

屏幕1上的按钮保持不变,屏幕2上的按钮因隐藏或显示的不同而有所不同,具体取决于在第一个屏幕上按下了哪个按钮。为此,我现在有一个很长的if语句,我试图找到减少它的方法。

可以帮助我,但我觉得我错过了一些简单的事情。

如果陈述与此类似:

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,因此我热衷于减少用于提高效率和可读性的代码量。

感谢。

3 个答案:

答案 0 :(得分:2)

您可以将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.")
}

答案 1 :(得分:2)

将所有选择的详细信息及其网址保留在字典中,例如

let selectionInfo = ["Detail A": optionOneData.detailA, "Detail E": optionOneData.detailE]

并使用此选项获取所选网址。

selectedURL = selectionInfo[selectedDetail]

如果selectedURL为零,则没有有效选择。

答案 2 :(得分:0)

从我在代码中看到的内容Switch()语句最适合您,而不是使用此长if-else语句。在这种情况下,这是最好的做法。