I am running low on energy and time complete this project now, I have done everything I can to learn and understand and implement a solution with no success, I would really like some help with this. Anyone got any ideas for implementing a solution?
I am really struggling to parse a json file into my iOS app in Swift. I have a JSON output from an SQL db. I think the formatting is wrong and the data headers are a bit ambiguous but my developer who I hired to create the process is happy with the output. Link below.
So in the last 3 days I have read all of the Apple Developer materials on parsing JSON, also watched numerous tutorials which all cover pretty much the same data, and I don't know how much I have read and converted into actual projects which have all failed. I am really needing some guidance here. I have even in a desperate attempt to understand my problem from a different perspective I have looked at how it is done in other languages. This is the latest iteration:
import UIKit import Foundation
struct ScheduleList: Codable {
let data: [Datum]
enum CodingKeys: String, CodingKey {
case data = "data"
}
}
struct Datum: Codable {
let date: String
let message: String
let notice: String
enum CodingKeys: String, CodingKey {
case date = "date"
case message = "message"
case notice = "notice"
}
}
class VCMainViewController: UIViewController {
@IBOutlet weak var flightTable: UITableView!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var route: UILabel!
@IBOutlet weak var std: UILabel!
@IBOutlet weak var sta: UILabel!
@IBOutlet weak var pax: UILabel!
let flight = [Datum]()
func parse() {
let jsonUrlString = "http://35.237.114.234/api/index.php?uid=Bx7faf08A9fYJ7bZCNMUX9EzxYN2"
guard let url = URL(string: jsonUrlString) else { return }
let task = URLSession.shared.scheduleListTask(with: url) { (scheduleList, response, error) in
guard let scheduleList = scheduleList, error == nil, response != nil else {
print("Something Wrong")
return
}
print("downlaoded")
do {
let decoder = JSONDecoder()
//Error Here: Cannot convert value of type 'ScheduleList' to expected argument type 'Data'
let downloadedFlights = try decoder.decode([scheduleList], from: scheduleList)
self.flight = downloadedFlights.date
} catch {
print("Something wrong after loading")
}
}
//I find I can print data / scheduleList here and it still returns correct data. So something above I guess is the problem.
task.resume()
/*
if scheduleList != nil {
print(scheduleList!)
} else {
print(error)
}
}
*/
}
override func viewDidLoad() {
super.viewDidLoad()
parse()
}
}
**Added full JSON file now due to issue with original post.
I think the structure is the problem. Any advice where I am going wrong? I can't cast that as Data, the dataTask data is called scheduleList so I don't know why this won't accept it.
Thanks