为什么alamofire总会返回失败案例?

时间:2017-03-27 15:54:25

标签: ios swift3

我正在尝试从网址获取数据。我给出了成功和失败的两个案例。但总是给我失败的情况。我不知道为什么总是把失败的情况归还给我..提前谢谢。

RegisterTwo

在本课程中,我试图下载数据。

import UIKit
import Alamofire

class RegisterTwo {

    private static var _businessName = [String]()

   public static var businessName: [String] {
        if _businessName.isEmpty == true {
            _businessName = ["Line of business"]
        }
        return _businessName
    }

    func downloadLineOfBusiness(completed: @escaping DownloadComplete) {

        let lineOfBusinessURL = URL(string: LINE_OF_BUSINESS_URL)!
        Alamofire.request(lineOfBusinessURL).responseJSON { (response) in
            let result = response.result

            switch(result) {

            case.success(_):

                if let dict = result.value as? Dictionary<String, AnyObject> {

                    // results
                    if let results = dict["results"] as? [Dictionary<String, AnyObject>] , results.count > 0 {

                        // name
                        if let name = results[0]["name"] as? String {

                            RegisterTwo._businessName.append("\(name)")
                        }

                        if results.count > 1 {

                            for x in 1..<results.count {
                                if let name = results[x]["name"] as? String {
                                    RegisterTwo._businessName.append("\(name)")
                                }
                            }

                        }
                        print(RegisterTwo._businessName)

                    }

                    // set the success value true
                    UserDefaults.standard.setIsSuccessLineOfBusiness(value: true)

                }

                break

            case.failure(_):

                // set the success value false
                UserDefaults.standard.setIsSuccessLineOfBusiness(value: false)
                print("Failure occured")

                break

            }


            completed()
        }

    }
}

SplashVC

我在这里调用这个函数。

override func viewDidAppear(_ animated: Bool) {
        checkInternet()
    }

    func checkInternet() {
        if currentReachabilityStatus == .reachableViaWiFi {
            print("User is connected to the internet via wifi.")

                self.registerTwo.downloadLineOfBusiness {

                    if self.isSuccessLineOfBusiness() {

                        self.perform(#selector(SplashVC.showVC), with: nil, afterDelay: 2)

                    } else {

                        self.alertBox(title: "Internet Error", message: "\nConnection Problem. Please reopen the app.")

                    }

                }

        }else if currentReachabilityStatus == .reachableViaWWAN{
            print("User is connected to the internet via WWAN.")

                self.registerTwo.downloadLineOfBusiness {

                    if self.isSuccessLineOfBusiness() {

                        self.perform(#selector(SplashVC.showVC), with: nil, afterDelay: 2)

                    } else {

                        self.alertBox(title: "Internet Error", message: "\nConnection Problem. Please reopen the app.")

                    }

                }

        } else {
            print("There is no internet connection")
            alertBox(title: "Internet Error", message: "\nNo internet connection.")
        }
    }

0 个答案:

没有答案