gpt4 book ai didi

ios - 检查 string == "(any of multiple strings)"{} 的最佳方法

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:29 25 4
gpt4 key购买 nike

我需要一种快速优化的方法来创建检查字符串与多个字符串的 if else 语句

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ANY OF MULTIPLE STRINGS 'x1'-'x9'"{
let JVC = segue.destinationViewController as VC3
JVC.betSource = segue.identifier!
} else {
let KVC = segue.destinationViewController as VC2
KVC.source = segue.identifier!
}

我应该使用 Array:string,做 9 个不同的 if/else 还是完全不同的东西?

我不知道什么可以最优化地运行代码。请指教

最佳答案

最佳方法是将可能的匹配项组成一个数组,然后使用 contains 在该数组中查找特定字符串。

let array = ["a", "b", "c"]

if contains(array, segue.identifier) {
// String found in array
}

关于ios - 检查 string == "(any of multiple strings)"{} 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com