Can we use keywords as parameter names in SWIFT?

好久不见. 提交于 2019-11-27 08:52:59

问题


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:


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
    }
}


来源:https://stackoverflow.com/questions/47212853/can-we-use-keywords-as-parameter-names-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!