在swift中找到数据时如何摆脱while循环?

时间:2016-07-27 09:44:18

标签: ios swift

我已编写以下代码以在while循环中检索data by date。问题是,当找到数据时,它没有退出循环。我想,我错过了一些代码来进行额外的检查。

因为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: [])!
}

2 个答案:

答案 0 :(得分:1)

您正在循环中进行异步请求,并且您的isFound变量在闭包内设置,因此循环不会等待响应。在您获得第一个回复之前,您的循环很可能会完成。

答案 1 :(得分:1)

虽然循环以sync方式工作,而api调用始终在后台运行,但意味着async,因此您需要像这样更改代码

func getData(startDate: NSDate) {
    if (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 {
                            JHProgressHUD.sharedHUD.hide()
                        }
                        else {
                            JHProgressHUD.sharedHUD.hide()
                            self.showPopup(Config.showPopupMsg)
                            startDate = calendar.dateByAddingUnit(.Day, value: 1, toDate: startdate, options: [])!
                            self.getData(startDate)
                        }
                    }
                    else {
                        JHProgressHUD.sharedHUD.hide()
                        self.showPopup(Config.showPopupMsg)
                        startDate = calendar.dateByAddingUnit(.Day, value: 1, toDate: startdate, options: [])!
                        self.getData(startDate)
                    }
                }
                else {
                    JHProgressHUD.sharedHUD.hide()
                }
            }
            else {
                JHProgressHUD.sharedHUD.hide()
            }
        }

    }
}