【问题标题】:Error with Json: Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}Json 错误:错误域 = NSCocoaErrorDomain 代码 = 3840“无值”。 UserInfo={NSDebugDescription=无值。}
【发布时间】:2017-01-31 12:18:38
【问题描述】:

您好,我是 Swift 3 和 iOS 开发的新手,我需要下载这个 php 文件提供的 JSONObject(一个数组):

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){
    include 'connectioncm.php';
    showMessage();
}
function showMessage()
{
    global $connect;
    $query = "SELECT * FROM `message` ORDER BY `message`.`id` DESC; ";

    $result = mysqli_query($connect, $query);
    $number_of_rows = mysqli_num_rows($result);

    $temp_array = array();

    if($number_of_rows > 0){
        while ($row = mysqli_fetch_assoc($result)){
            $temp_array[] = $row;

        }
    }
    header('Content-Type: application/json');
    echo json_encode(array("message"=>$temp_array));
    mysqli_close($connect);
}

这是我的 viewDidLoad 函数:

override func viewDidLoad() {
 super.viewDidLoad()
    let requestURL: NSURL = NSURL(string: "http://cm.890m.com/newsfeedshow.php")!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {
        (data, response, error) -> Void in

        let httpResponse = response as! HTTPURLResponse
        let statusCode = httpResponse.statusCode

        if (statusCode == 200) {
            print("Everyone is fine, file downloaded successfully.")
        }
        do{

            let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)

            if let message = (json as?NSDictionary)?["message"] as? [[String: AnyObject]] {

                for station in message {

                    if let deleg = (station["messagedeleg"] as? String) {

                        if let name = (station["nomdeleg"] as? String) {
                            self.names.append(name)
                            self.post.append(deleg)


                        }

                    }
                }

            }

        }catch {
            print("Error with Json: \(error)")
        }
    }

    task.resume()
}

谁产生了这个错误:

Json 出错:错误域 = NSCocoaErrorDomain 代码 = 3840 “无值”。 UserInfo={NSDebugDescription=无值。}

感谢您的帮助,我在这里真的很挣扎。

伊兰·罗斯勒。

【问题讨论】:

    标签: php ios json swift nsurlrequest


    【解决方案1】:

    这是断点产生的错误。

    希望它可以帮助另一个 Swift 新手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      相关资源
      最近更新 更多