Navigation controller in Swift I was looking for a way to write this code shorter and still get the same result. Can this be achieved?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if indexPath.row == 0 {
        print("connection status pressed")

        print("get")

        let connectionStatusSelectorViewController = ConnectionStatusSelector()
        //profileEditor.user = self.user
        navigationController?.pushViewController(connectionStatusSelectorViewController, animated: true)

    }else{
        if indexPath.row == 1 {
            print("account type pressed")


            let AccountSelectorViewController = AccountTypeSelector()

            navigationController?.pushViewController(AccountSelectorViewController, animated: true)


        }else{
            if indexPath.row == 2 {
                print("gender type pressed")

                let GenderSelectorViewController = GenderTypeSelector()
                navigationController?.pushViewController(GenderSelectorViewController, animated: true)

            }else{
                if indexPath.row == 3 {
                    print("service type pressed")

                    let serviceSelectorViewController = ServiceTypeSelector()
                    navigationController?.pushViewController(serviceSelectorViewController, animated: true)

                }else{
                    if indexPath.row == 4 {
                        print("distance pressed")

                    }else{
        print("nothing")
}
}
}
}
}
}
One simple thing that would clean this up is to use the else if keyword. So you would do:

if indexPath.row == 0
{
    // code here
}
else if indexPath.row == 1
{
    // code for 1 here
}
else if indexPath.row == 2
{
    // code for 2 here
}
else if indexPath.row == 3
{
    ...etc.
}


This eliminates the nesting and the excessive number of closing curly braces at the end.

You could also remove the intermediate values. So one of the bodies of the "if" statement might look like this:

if indexPath.row == 2
{
    print("gender type pressed")
    navigationController?.pushViewController(GenderTypeSelector(), animated: true)
}