在Swift中for循环的声明中是否可以直接排除某个值或对象?我正在尝试做这样的事情,在其中循环索引,但我想在同一行中过滤掉 someIndex
。
func updateTabsEnabled(isShowing: Bool) {
for index in 0..<viewControllers!.count, index != someIndex {
tabBar.items![index].isEnabled = !isShowing
}
}
我认为这在 Swift 3 中是可能的,虽然我不记得了 documentation里面似乎没有,虽然我可能错过了。
最佳答案
需要使用where
关键字,然后添加条件
func updateTabsEnabled(isShowing: Bool) {
for index in 0..<viewControllers!.count where index != someIndex {
tabBar.items![index].isEnabled = !isShowing
}
}
关于Swift 3 在 For 循环中排除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42212988/