我寫了下面的代碼來檢索while循環中的data by date
。問題是,當數據被發現時,它不會退出循環。我想,我錯過了一些代碼來進行額外的檢查。如何在swift中找到數據時跳出while循環?
因爲print
正在多次工作。請幫我解決。
while startdate.compare(enddate) != .OrderedDescending {
let mydate = Config.convertDateTimeFormater(Config.convertDateToString(startdate, fromFormat:
let test = [
"date" : mydate
]
JHProgressHUD.sharedHUD.showInView(self.view, withHeader: "Loading", andFooter: "Please wait")
Alamofire.request(.POST, Config.checkBookingSlot, parameters: test as? [String : AnyObject], headers: headers, encoding: .JSON).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
let msg = swiftyJsonVar["message"].stringValue
if (swiftyJsonVar["success"]) {
if let slotData = swiftyJsonVar["data"]["detail"].arrayObject {
if slotData.count > 0 {
isFound = true
print("Break")
JHProgressHUD.sharedHUD.hide()
} else {
JHProgressHUD.sharedHUD.hide()
self.showPopup(Config.showPopupMsg)
}
} else {
JHProgressHUD.sharedHUD.hide()
self.showPopup(Config.showPopupMsg)
}
} else {
JHProgressHUD.sharedHUD.hide()
isFound = true
}
} else {
JHProgressHUD.sharedHUD.hide()
isFound = true
}
}
if isFound {
break
}
startdate = calendar.dateByAddingUnit(.Day, value: 1, toDate: startdate, options: [])!
}
您的代碼中看到這個曾經u得到的想法http://stackoverflow.com/questions/ 9418972/ios-break-nested-loop –
而不是將網絡調用放入循環,使用'NStimer'或'Performselector'任務,以便您能夠通過響應檢查您的狀況。否則你的循環會在你得到第一個響應之前完成。 –
你確定你正在接收和解析JSON數據嗎? – Feldur