0
你好,我是很新的斯威夫特3和一般的iOS開發,我需要下載這個PHP文件中提供的的JSONObject(數組):Json出錯:錯誤域= NSCocoaErrorDomain代碼= 3840「沒有值。」的UserInfo = {NSDebugDescription =無值。}
<?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 =沒有值。}
感謝您的幫助,我真的在這裏掙扎。
Ilan Rossler。