Json出错:错误域= NSCocoaErrorDomain代码= 3840"没有值。" UserInfo = {NSDebugDescription =无值。}

时间:2016-09-22 19:15:40

标签: php ios json swift nsurlrequest

你好我在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 Code = 3840&#34;没有值。&#34; UserInfo = {NSDebugDescription =无值。}

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

Ilan Rossler。

1 个答案:

答案 0 :(得分:0)

这是断点产生的错误。

希望它能帮助Swift中的另一个新手。