汇总并存储for语句中的数据

时间:2019-05-31 14:47:29

标签: objective-c

我需要记住此FOR语句中的数据

for (UTEModelSportWalkRun *model in arraySport) {
    NSLog(@"sport *** time = %@, Total step = %ld, walkDistance = %f , walkCalorie = %f, runDistance = %f, runCalorie = %f",
    model.time, (long)model.stepsTotal, model.walkDistances, model.walkCalories, model.runDistances, model.runCalories);
}

我需要将所有model.stepTotal相加并以某种方式存储总和。现在出来像这样:

  

体育***时间= 2019-05-30-17,总步长= 14,walkDistance = 9.764355,walkCalorie = 0.346086,runDistance = 0.000000,runCalories = 0.000000
  运动***时间= 2019-05-31-10,总步长= 67,步行距离= 46.729411,步行卡路里= 1.656267,跑步距离= 0.000000,跑步卡路里= 0.000000
  运动***时间= 2019-05-31-11,总步长= 520,步行距离= 362.676028,步行卡路里= 12.854608,   runDistance = 0.000000,runCalories = 0.000000

.....

1 个答案:

答案 0 :(得分:0)

long count = 0;
for (UTEModelSportWalkRun *model in arraySport) {
      count += (long)model.stepsTotal;
}

在循环之前声明变量,并在之后进行访问。称为作用域。