为什么如果条件正确我会一直为零

时间:2019-01-12 00:07:40

标签: json swift parsing

条件成功后,我会一直显示NIL和错误

我有这个PHP

<?php
require_once 'hehe';
error_reporting(0);
$sql = "SELECT Precio FROM Solicitudes where ID_Pasajero = '".$_POST['ID_Pasajero']."' and Estado = '1' ORDER BY ID_Solicitudes DESC LIMIT 1";
$result = $conexion->query($sql);
if ($result->num_rows > 0) {
    $count = $result->num_rows;
    $row = mysqli_fetch_array($result);
    echo '{
    "Response":"true",
    "Perfil":[{
        "Precio":"'.$row['0'].'"
    }]
}';

}else{
    echo '{
    "Response":"false",
    "Perfil":[{
        "Precio":"false"
    }]
}';
}


mysqli_close($conexion);
?>

这是我的快捷代码

@objc func ConsultarSiAcepto(){

    let idpa = UserDefaults.standard.string(forKey: "idUser")

    let myURL = URL(string: "hee hee")
    var request = URLRequest(url: myURL!)
    request.httpMethod = "POST"
    let posString = "ID_Pasajero=\(idpa!)"
    request.httpBody = posString.data(using: .utf8)
    let task = URLSession.shared.dataTask(with: request) {
        data, response, error in

        if let error = error {
            print("error=\(error)")
            return
        }

        guard let data = data else {
            print("Something wrong")
            return
        }

        struct Blog: Decodable {
            let Response: String
            let articles: [Article]

            enum CodingKeys : String, CodingKey {
                case Response

                case articles = "Perfil"
            }
        }

        struct Article: Decodable {
            let Precio: String

        }

        do {
            let blog = try JSONDecoder().decode(Blog.self, from: data)


            DispatchQueue.main.async {
                if blog.Response == "true" {
                    print(data)
                    print(response)
                    print("el conductor acepto el viaje.. enviando cotizacion")
                    for article in blog.articles {

                        let precio = article.Precio
                        self.elconductorcobra.text = "Costo del viaje \(precio)"

                        break
                    }

                }

                if blog.Response == "false" {

                    print("esperando a que el conductor envie la cotizacion...")

                }
                else {
                    print(error)
                    print("error hapend :0")

                }

            } //aqui termina el dispatch main
        }

        catch {
            print("Error: Couldn't decode data into Blog:", error)
            print(error)
            return

        }

    }
    task.resume()


}

我用这个代码伪装成定时器

self.helloWorldTimer = Timer.scheduledTimer(timeInterval: 15.0, target: self, selector: #selector(UnaVezLogeadoViewController.ConsultarSiAcepto), userInfo: nil, repeats: true)

询问驱动程序是否已接受该请求,因此代码每15秒将此PHP发送到我的数据库

我不知道缺少什么,因为我是从控制台获得此响应的

esperando a que el conductor envie la cotizacion...
esperando a que el conductor envie la cotizacion...
96 bytes
Optional(<NSHTTPURLResponse: 0x16790b30> { URL:     hehe } {    status code: 200, headers {
"Content-Encoding" = gzip;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Fri, 11 Jan 2019 23:51:59 GMT";
Server = Apache;
"x-powered-by" = "PHP/7.1.25";
 } })
el conductor acepto el viaje.. enviando cotizacion
nil
error hapend :0

我让计时器运行2次(30秒),以查看代码是否在驱动程序的HOLD上以放置一些信息

'esperando a que el conductor envie la cotizacion...'

所以它在HOLD上工作

if blog.Response == "false" {

                    print("esperando a que el conductor envie la cotizacion...")

                }

如您所见,代码获取了正确的值

96 bytes
Optional(<NSHTTPURLResponse: 0x16790b30> { URL:     hehe } {    status code: 200, headers {
"Content-Encoding" = gzip;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Fri, 11 Jan 2019 23:51:59 GMT";
Server = Apache;
"x-powered-by" = "PHP/7.1.25";
 } })
el conductor acepto el viaje.. enviando cotizacion

这是代码显示错误/解析的地方

DispatchQueue.main.async {
                if blog.Response == "true" {
                    print(data)
                    print(response)
                    print("el conductor acepto el viaje.. enviando cotizacion")
                    for article in blog.articles {

                        let precio = article.Precio
                        self.elconductorcobra.text = "Costo del viaje \(precio)" // hereeeeeee

                        break
                    }

                }

                if blog.Response == "false" {

                    print("esperando a que el conductor envie la cotizacion...")

                }
                else {
                    print(error)
                    print("error hapend :0")

                }

            }

信息在编写代码时正确填写 我并没有在屏幕或功能上出现任何错误,我只能因为该错误而无法继续操作,还是可以避免吗?

我已经尝试通过许多不同的方法来更改我的PHP来更改JSON响应并避免此NIL错误

1 个答案:

答案 0 :(得分:0)

问题解决。

我制作了另一个VC,默认情况下启动该功能,现在可以使用