Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Keyword in Swift

Tags:

swift

I know that the keyword "pass" in Python will allow one to leave a line of code empty where it should have contained an executable statement. Is there a similar keyword in Swift?

I am using a switch statement and Swift requires there to be a default case. The code should reach the default statement most of the time and I want nothing to be done in this case.

like image 907
Acoop Avatar asked Dec 04 '14 22:12

Acoop


2 Answers

You can break out of the default case. Swift just wants you to be explicit about that to avoid bugs.

Here's a simple example:

enum Food {
    case Banana
    case Apple
    case ChocolateBar
}

func warnIfUnhealthy(food : Food) {
    switch food {
    case .ChocolateBar:
        println("Don't eat it!")

    default:
        break
    }

}

let candy = Food.ChocolateBar

warnIfUnhealthy(candy)
like image 82
Aaron Brager Avatar answered Nov 08 '22 14:11

Aaron Brager


The proper way to add a catch-all without an action to a switch statement is to add

default: break

at the end.

like image 24
Mundi Avatar answered Nov 08 '22 16:11

Mundi