Can we use keywords as parameter names in SWIFT?

2019-01-20 20:20发布

Basically, I want to set up a function that uses 'for' as a parameter for readability.

enum Genre {
    case drama
    case comedy
}

func setupTable(for: Genre) {
    switch for {
    case .drama: break
    case .comedy: break
    }
}

I set something like this up but when i try and use the switch for 'for' it comes up as a keyword and throws a compile error.

Cheers

1条回答
Ridiculous、
2楼-- · 2019-01-20 21:03

When using a keyword as a normal identifier you have to escape it using backticks ` like this

func setupTable(for: Genre) {
    switch `for` {
        case .drama: break
        case .comedy: break
    }
}
查看更多
登录 后发表回答