Ios 等同于UILabel访问的Swift

Ios 等同于UILabel访问的Swift,ios,swift,cocoa-touch,Ios,Swift,Cocoa Touch,这在目标C中对我有效,但我无法在Swift中实现: - (IBAction)acceptWeight:(UIButton *)sender { int tempValue = (int) currentWeight; // current weight comes from a UISegementedController for (UILabel *labels in self.view.subviews) { if (labels.tag == curr

这在目标C中对我有效,但我无法在Swift中实现:

- (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];
}
我尝试动态地访问许多UILabel中的一个,并更新其内容

我在这里试过一种工具,虽然它是一种勇敢的转换尝试,但它并没有成功

它给

labels.text = [NSString stringWithFormat:@"%i",bags[tempValue]];
与此等效:

labels.text = "\(bags[tempValue])"
// compiler warns.. Cannot assign to 'text' in 'labels'
披露:这是基于我在这里提出的一个问题: (最终没有一个答案对我有帮助,所以我最终尝试了自己的方法。)我最近所有与swift相关的搜索都讨论了不太合适的方法


编辑: 为了完整性[并按要求]这里是上下文中完整的swift代码函数

    @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)"
}

假设所有子视图都是UILabel。只要你添加一个按钮,它就会断开。试试这个:

@IBAction func acceptWeight(sender: UIButton)  {
    var tempValue = currentWeight

    for subview in self.view.subviews {
        if let label = subview as UILabel where label.tag == currentWeight {
          bags[tempValue] += 1
          label.text = "\(bags[tempValue])"
      }
   }
   totalKilo = totalKilo + currentWeight
   self.totalKilo.text = "\(totalKilo)"
}

您发布的Objective-C代码没有帮助,也没有添加到讨论中。如果您需要解决此问题的任何帮助,请尝试使用实际的完整Swift实现替换该代码。您发布了一行Swift和一条错误消息。您尚未向我们显示错误出现的上下文。你让我们猜猜上下文是的!成功了!!但是您知道视图中其他UI元素的标记值是正确的。奇怪的是,我挑出来的一句话现在能用了,而以前没用。我只是想弄清楚,当它们不适用于If(…)条件时,如何优雅地清除它们的文本。谢谢