如果在swift3中没有返回任何内容

时间:2016-09-19 21:51:33

标签: swift alamofire

我在swift3中有一个函数,它获取已批准的变量并检查它是否为真。如果它是真的那么它将显示警告,但如果不是真的,那么当应该显示批准被拒绝的警报时没有任何反应。当我尝试用简单的print()

替换警报时,它甚至不会打印任何内容

可能是什么问题?

@IBAction func approveuser(_ sender: AnyObject) {
    theplace = place //Saving variable.


    DispatchQueue.main.async {

        if(self.place == ""){
            self.place = self.pickerData[0]
        }

        //BEGIN CHECK.
        Alamofire.request("https://example.com/api.php?appvar=\(appvar)&requested=\(self.place)").responseJSON{ (response) -> Void in

            if let JSON = response.result.value{
                let json = JSON as! NSDictionary
                Approved = json["Approved"] as! String
                let version = json["version"] as! String

                if(version != AppVersion){
                    self.showalert("Update Required", message: "A new version is available. Please update!", confirm: "Okay")
                    return
                }

                if(Approved == "true"){
                    theurl = json["URL"] as! String //Give the URL.
                    self.showalert("Approved!", message: "Approved", confirm: "Okay")
                }else{
                    self.showalert("Denied!", message: "Denied", confirm: "Okay")
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

这一行之后

Approved = json["Approved"] as! String

打印已批准

print(Approved)

确保打印为true或false 确保它不打印可选(true)或可选(false)

升级到Swift 3后的json响应会返回许多可选值,即使从Swift 2升级后您的代码中没有更改任何内容