I'm trying to convert my old .js api call into my swift app. In the .js I had to send a query with the api url to decide which fields to receive back.
Like this "https://api.myapi.com/planner?query=" + content.txt
content.txt
{
places(
ids:["ABC:StopPlace:8329", "ABC:StopPlace:9237"]
) {
name
id
estimatedCalls(timeRange: 72100, numberOfDepartures: 20) {
realtime
realtimeState
expectedDepartureTime
predictionInaccurate
destinationDisplay {
frontText
}
}
}
}
Right now I'm trying this:
func loadR(filename: String ) -> String {
let url = Bundle.main.url(forResource: filename, withExtension: "txt")!
let data = try! Data(contentsOf: url)
let string = String(data: data, encoding: .utf8)!
print(string)
self.load(q: string)
return string
}
func load(q: String) {
var urlQ = "https://api.apisite.com/planner?query=" + q
let url = URL(string: urlQ)!
URLSession.shared.dataTask(with: url) {(data,resp,error) in
do {
if let d = data {
print(data)
/*
let decodedLists = try JSONDecoder().decode([Weather].self, from: d)
DispatchQueue.main.async {
self.metroTimes = decodedLists
}
*/
}else {
print("No Data")
}
} catch {
print ("Error")
print(error)
}
}.resume()
}
but it throws an error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value:"
What is the correct way to parse this file into a url string?