首页 > 解决方案 > 如果条件正确,为什么我一直为零

问题描述

条件成功后,我不断收到 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 错误

标签: jsonswiftparsing

解决方案


问题解决。

我制作了一个不同的VC,它默认启动该功能,现在可以工作


推荐阅读