Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 循环中的闭包?_Ios_Loops_Swift3_Closures - Fatal编程技术网

Ios 循环中的闭包?

Ios 循环中的闭包?,ios,loops,swift3,closures,Ios,Loops,Swift3,Closures,我对此代码有一些问题: let earlyDate = startDate.dateByAddingHours(-12) var currentDayInt = earlyDate.daysBeforeDate(Date()) + 1; var currentDayDate = Podometer.getMidnight(Date()); var currentDayDateServer = Podometer.getNoon(Date()); var wantedDateEnd = curre

我对此代码有一些问题:

let earlyDate = startDate.dateByAddingHours(-12)
var currentDayInt = earlyDate.daysBeforeDate(Date()) + 1;
var currentDayDate = Podometer.getMidnight(Date());
var currentDayDateServer = Podometer.getNoon(Date());
var wantedDateEnd = currentDayDate.dateByAddingHours(24)

for i in (0..<currentDayInt).reversed() {
   self.getSteps(startDate, challengeEndDate: endDate, wantedDateStart:currentDayDate, wantedDateEnd: wantedDateEnd, completion: { (stepNumber) -> Void in
         self.realmSteps.cacheSteps(user_id: self.user_id, day: i + 1, date: currentDayDateServer, nb_steps: Int(stepNumber))
   })
   wantedDateEnd = wantedDateEnd.dateByAddingHours(-24)
   currentDayDateServer = currentDayDateServer.dateByAddingHours(-24)
   currentDayDate = currentDayDate.dateByAddingHours(-24)
}
让earlyDate=startDate.dateByAddingHours(-12)
var currentDayInt=earlyDate.daysBeforeDate(Date())+1;
var currentDayDate=Podometer.getMidnight(Date());
var currentDayDateServer=Podometer.getNoon(Date());
var wantedDateEnd=currentDayDate.dateByAddingHours(24)
对于i in(0..Void in
self.realmSteps.cacheSteps(用户id:self.user\id,日期:i+1,日期:currentDayDateServer,nb\u步骤:Int(步骤号))
})
wantedDateEnd=wantedDateEnd.dateByAddingHours(-24)
currentDayDateServer=currentDayDateServer.dateByAddingHours(-24)
currentDayDate=currentDayDate.dateByAddingHours(-24)
}
我正试图在一天的时间内从iPhone计步器中获得步幅。这是可行的,但问题来自循环中的日期


它们被奇怪地更新,我的缓存做了一些奇怪的事情。我认为这是因为我在循环中使用了闭包。我怎样才能解决它?(如果我在闭包中添加了一个新值,它们不会被更新)。

这是因为在执行闭包之前,可能会调用您的三行结束符。您应该通过在完成闭包中返回它来重新使用在闭包中发送的日期,以更新您的域,不要使用变量outside@Swift_Guru没有改变,除了更多bug@Tj3n不太清楚,我必须将所有日期发送返回到getStep闭包,并在闭包正文中更新此返回值?我的意思是将闭包中的
currentDayDateServer
作为一个单独的变量发送,然后将其与
步骤号一起返回以更新您的领域,它将生成正确的日期