title: Swift equivalent of UILabel access
body: <p>This worked for me in Objective C, but i can't for the life of me get it to work in Swift: </p>
  
  <pre><code>- (IBAction)acceptWeight:(UIButton *)sender {
     int tempValue = (int) currentWeight;
    // current weight comes from a UISegementedController
  
     for (UILabel *labels in self.view.subviews) 
     {
        if (labels.tag == currentWeight) 
        {
           bags[tempValue]++;
           labels.text = [NSString stringWithFormat:@"%i",bags[tempValue]];
        }
     }
    totalKilo = totalKilo + (int)currentWeight;
    self.totalKilo.text = [NSString stringWithFormat:@"%d",totalKilo];
  }
  </code></pre>
  
  <p>I'm trying to generically access one of a number of UILabels dynamically, and update its contents.</p>
  
  <p>There is a tool i tried here <a href="http://www.ojectivec2swift.net" rel="nofollow noreferrer">ojectivec2swift.net</a> but while t'was a brave attempt at conversion, it didn't cut the mustard</p>
  
  <p>it gives</p>
  
  <pre><code>labels.text = [NSString stringWithFormat:@"%i",bags[tempValue]];
  </code></pre>
  
  <p>as this equivalent:</p>
  
  <pre><code>labels.text = "\(bags[tempValue])"
  // compiler warns.. Cannot assign to 'text' in 'labels'
  </code></pre>
  
  <p>disclosure: this is based on a question i asked here:
  <a href="https://stackoverflow.com/questions/22649867/iphone-how-to-select-from-a-collection-of-uilabels/22649968#22649968">iPhone - how to select from a collection of UILabels?</a>
  ( and in the end none of the answers quite did it for me so i eventually experimented my way round. ) All my recent swift-related searches discuss not-quite-fitting approaches</p>
  
  <hr>
  
  <p>EDIT:
  for the sake of completeness [and as requested] here's the whole swift code function in context</p>
  
  <pre><code>    @IBAction func acceptWeight(sender: UIButton)  {
      var tempValue: Int = currentWeight
  
      for labels: UILabel in self.view.subviews {
          if labels.tag == currentWeight {
            bags[tempValue]++
            labels.text = "\(bags[tempValue])"
        }
     }
     totalKilo = totalKilo + currentWeight
      self.totalKilo.text = "\(totalKilo)"
  }
  </code></pre>