我们可以在SWIFT中使用关键字作为参数名称吗? [英] Can we use keywords as parameter names in SWIFT?
本文介绍了我们可以在SWIFT中使用关键字作为参数名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上,我想设置一个使用'for'作为参数以提高可读性的函数.
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
}
}
我进行了类似的设置,但是当我尝试将开关用于"for"时,它将作为关键字出现并引发编译错误.
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.
欢呼
推荐答案
将关键字用作普通标识符时,您必须使用反引号`来将其转义,
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
}
}
这篇关于我们可以在SWIFT中使用关键字作为参数名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文