NSJSONSerialization在结束时给出了垃圾错误3840

时间:2016-09-09 04:41:19

标签: php mysql json swift

当我尝试从php页面获取数据时,我在swift上遇到此Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}错误

这是php代码

require ('database.php');
function get_msg()
{
    global $connection;
    $query = "SELECT `sender`,`message` FROM `simply`.`chat_experimetn` WHERE `sender` = 'ken' || `sender` = 'jen' ";

$result = mysqli_query($connection,$query);

$messages = array();
while($message = mysqli_fetch_assoc($result))
{
    $messages[] = array('sender'=>$message['sender'],'message'=>$message['message']);
}


echo json_encode($messages);
return $messages;

} this is what the array look like in localhost

这是快速代码

var values:NSArray = []

override func viewDidLoad() {
    super.viewDidLoad()
    get();
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func get(){
    let url = NSURL(string: "http://localhost/training_site/php/index.php")
    let data = NSData(contentsOfURL: url!)
    do{
    values = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
    tableView.reloadData()
    }catch{
        print(error)
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SpecialCell
    let maindata = values[indexPath.row]
    cell.username.text = maindata["sender"] as? String
    cell.password.text = maindata["message"] as? String
    //cell.num.text = maindata["FavoriteNumber"] as? String
    //cell.info.text = maindata["Info"] as? String
    return cell;

}

我无法找到解决方案。这是我最后的希望,请帮助。

0 个答案:

没有答案