这行得通,但我正试图找出一种更好的方式来编写它。我想替换这个 if 条件。我有多个这样的案例
if text == "a" || text == "ac" || text == "acc" || text == "acco" || text == "accou"
|| text == "accoun" || text == "account"
{
flagAccount = false
} else {
flagAccount = true
}
Run Code Online (Sandbox Code Playgroud)
我想过使用 CharacterSet 但似乎无法弄清楚
条件等价于
"account".hasPrefix(text) && text != ""
Run Code Online (Sandbox Code Playgroud)
注意帽子text != ""是必需的,因为它"account".hasPrefix("")是真的。
正如 Rob 建议的那样,您可以在这样的 if 语句中编写此内容,使用!text.isEmpty可能更具可读性:
if !text.isEmpty, "account".hasPrefix(text) {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |