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.

4
  • Having that many $0?. is a pretty clear sign that you should unwrap $0 and assign it to a local, non-optional variable Commented Jan 29, 2018 at 3:37
  • use for (index, value) in array.enumerated() { } Commented Jan 29, 2018 at 3:38
  • It definitely not duplicate cus no where i could able to find answer Commented Jan 29, 2018 at 3:42
  • @PeterGuo I suggest you read it again, more carefully, then Commented Jan 29, 2018 at 3:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.