Hello i'm pretty new in Swift 3 and iOS development in general, i need to download the JSONObject (an array) provided by this php file :
<?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);
}
And this is my viewDidLoad function :
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()
}
Who generates this error :
Error with Json: Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}
Thanks for your help, i'm really struggling here.
Ilan Rossler.
It was an error generated by the breakpoints.
Hope it'll may help another newbie in Swift.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.