【发布时间】:2016-01-05 18:51:45
【问题描述】:
我在尝试将 Json 对象放入我的 iOS 应用程序时遇到了很多问题。我从 MysqlDB 中创建了一个 Json 文件,如下所示:
<?php
//open connection to mysql db
$connection = mysqli_connect("127.0.0.1","root","","FeedStuff") or die("Err$
//fetch table rows from mysql db
$sql = "select * from articles";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . my$
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysqli_close($connection);
?>
现在 json 对象看起来不错。 plato.guru.ksz.ch/index.php 将是对象。 这些是我加载到我的数据库中的新闻文章,它们每个都有一个 ID、标题、描述、类别、链接、媒体和来源,所有这些都只是字符串(除了 ID)
现在我尝试将此 Json 文件放入我的 iOS 对象中,以在表格单元格中显示每篇文章。 我遇到了问题,但我不太确定是 Json 文件本身还是我的 iOS 应用程序的连接。
这是 swift 文件:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let filePath = NSURL(string: "plato.guru.ksz.ch/index.php")
let jsonData = NSData(contentsOfURL:filePath!)
let json = JSON(data: (jsonData)!, options: NSJSONReadingOptions.AllowFragments, error: nil)
print(json)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我手动导入了 SwiftyJSON 文件,因为我无法让它与 pod 一起使用。 现在是我的 Json 文件不正确,或者为什么我在执行程序时收到此错误: 致命错误:在展开可选值时意外发现 nil (lldb)
感谢您的帮助,这几天一直被这个问题困扰。
【问题讨论】: