带有索引的数组迭代闭包在swift -iOS中

时间:2018-01-29 03:26:04

标签: arrays swift closures iteration

我想知道是否有一条捷径可以让代码变得简单?

  • 索引是从Foreach关闭而不是自己设置的自动增量(任何由库提供的关键字)?
  • 将“index”作为参数传递给我的onSectionSelected?

    var index = 0
    [view_a, view_b, view_c, view_d].forEach{
        $0?.isUserInteractionEnabled = true
        $0?.tag = index
        index += 1
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onSectionSelected))
        $0?.addGestureRecognizer(tapGesture)
    }
    
    func onSectionSelected() {
        //do something for touchUpInside event
    }
    

强调我清楚地了解如何使用(索引,值)数组。列举来表达这一点。请相应地回答。

0 个答案:

没有答案