Swift 3 在 For 循环中排除项目

标签 swift

在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/

相关文章:

ios - Swift 无法从@IBDesignable 类设置 View 的高度

ios - 如何从 TableView 中显示的搜索结果导航到另一个 View ?

swift - 返回核心数据中存储的从现在起 1 个月后发生的所有生日

Swift - 在 Struct 中使用#available 来初始化变量

ios - 以编程方式快速更改 UIButton 的文本

ios - 如何阻止搜索栏隐藏在滚动条上

ios - 从挂载 map 的数组中检索信息 (MapKit)

带有 UITableViewController 的 iOS 标签栏应用程序

ios - UITabelView 中带有 API 数据的部分

json - 在Swift 3中正确解析JSON