0

I am wondering Is there a shortcut to make the code following simple?

  • Index are getting from Foreach closure instead of auto-increment set by myself (any keyword provide by library)?
  • Passing "index" into my onSectionSelected as an argument?

    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
    }
    

Emphasise that I clearly understand how to use (index, value), array. enumerated to express this. Please answer accordingly.

Peter Guo
  • 182
  • 1
  • 11

0 Answers0