你好,我是 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=无值。}
谢谢你的帮助,我真的很苦恼。
伊兰·罗斯勒。
最佳答案
这是断点产生的错误。
希望它能对 Swift 的另一个新手有所帮助。
关于php - Json 错误 : Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value. },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39647198/