I'm making an API call and managing the data received, but my call is catching an error. Here's my getData() code:
func getData(from url: String) {
URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in
guard let data = data, error == nil else {
print("something went wrong.")
return
}
do {
self.instructionsResults = try JSONDecoder().decode([Step].self, from: data)
print("getData() was successful!")
print(self.instructionsResults)
} catch {
print("Decoding error:")
print(String(describing: error)) // <-- this pings
}
}).resume()
}
Here's a pastebin of an example url json data: link
And here's the struct I've defined for this fetch:
struct Step: Codable {
let number: Int
let step: String?
}
This may be extra, but I'm using the call above to populate the array instantiated as var steps: [String] = [] with the step: String data of each step in the JSON Step array.
for n: Int in 0 ..< instructionsResults.count {
if instructionsResults[n].step != nil {
let step = instructionsResults[n].step ?? "n/a"
print("step: \(instructionsResults[n].step)")
print("step: \(step)")
steps.append(step)
}
}
print("Steps: \(steps)")
}
Does anyone have any insight on what's going wrong? My final print statement always returns as empty. I've done a similar type of call formatted a similar way earlier in this project, and that worked completely fine, so I'm stumped as to where I went wrong with this one. Any insight / feedback would be greatly appreciated, thank you.
Edit: Here's the error code:
Steps: []
Decoding error:
keyNotFound(CodingKeys(stringValue: "number", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"number\", intValue: nil) (\"number\").", underlyingError: nil))
The error says that there is no key number in the top level object.
Please read the JSON carefully. You are ignoring the object on the root level, the array with the key steps.
You need this
struct Root: Decodable {
let steps: [Step]
}
struct Step: Decodable {
let number: Int
let step : String
}
and decode
.decode([Root].self,
Related
I am learning Swift and trying to get elevation data based on coordinates from the Open Elevation API.
I found a code to make the request and decode the data using structs.
My problem is that the API result includes the information in an array:
{"results": [{"latitude": 41.161758, "longitude": -8.583933, "elevation": 117}]}
What I have been able to program so far does save the data as an array in json.results, but only with one index including all of the data:
[API.MyResult(latitude: 41.16176, longitude: -8.583933, elevation: 117)]
("API" is the name of the file)
Here is my code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let url = "https://api.open-elevation.com/api/v1/lookup?locations=41.161758,-8.583933"
getData(from: url)
}
private func getData(from url: String){
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: {data, response, error in
guard let data = data, error == nil else {
print("error")
return
}
var result: Response?
//print(result)
do{
result = try JSONDecoder().decode(Response.self, from: data)
}
catch{
print(error.localizedDescription)
}
guard let json = result else {
return
}
print(json.results)
//print(json.results.latitude)
//print(json.results.longitude)
//print(json.results.elevation)
})
task.resume()
}
}
struct Response: Codable {
let results: [MyResult]
}
struct MyResult: Codable {
let latitude: Float
let longitude: Float
let elevation: Int
}
Trying to print out json.results.latitude leads to the error
"Value of type '[MyResult]' has no member 'latitude'"
I assume at some point, a variable has to be defined as an array.
What needs to be changed here?
result is indeed a single object, but the property results is an array (multiple objects).
A slightly different naming avoids the confusion.
Notes:
Never print literal "error" or error.localizedDescription in a Decoding context, always print the error instance.
Proceed to parse the result in the do scope
private func getData(from url: String){
guard let url = URL(string: url) else { print("Bad URL", url); return }
let task = URLSession.shared.dataTask(with: url) {data, _, error in
if let error = error { print(error); return }
do {
let response = try JSONDecoder().decode(Response.self, from: data!)
for result in response.results {
print(result.latitude)
print(result.longitude)
print(result.elevation)
}
}
catch {
print(error)
}
}
task.resume()
}
I'm new to Swift 5.3 and having trouble retrieving my nested JSON data.
My JSON data result looks like this:
{
"sites":[
{
"site_no":"16103000",
"station_nm":"Hanalei River nr Hanalei, Kauai, HI",
"dec_lat_va":22.1796,
"dec_long_va":-159.466,
"huc_cd":"20070000",
"tz_cd":"HST",
"flow":92.8,
"flow_unit":"cfs",
"flow_dt":"2020-08-18 07:10:00",
"stage":1.47,
"stage_unit":"ft",
"stage_dt":"2020-08-18 07:10:00",
"class":0,
"percentile":31.9,
"percent_median":"86.73",
"percent_mean":"50.77",
"url":"https:\/\/waterdata.usgs.gov\/hi\/nwis\/uv?site_no=16103000"
}
]
}
My structs look like this:
struct APIResponse: Codable {
let sites: APIResponseSites
}
struct APIResponseSites: Codable {
let station_nm: String
let stage: Float
}
And my Decode SWIFT looks like this:
let task = URLSession.shared.dataTask(with: url, completionHandler: {
data, _, error in
guard let data = data, error == nil else {
return
}
var result: APIResponse?
do {
result = try JSONDecoder().decode(APIResponse.self, from: data)
}
catch {
print("Failed to decode with error: \(error)")
}
guard let final = result else {
return
}
print(final.sites.station_nm)
print(final.sites.stage)
})
And of course, I get an error that states:
Failed to decode with error:
typeMismatch(Swift.Dictionary<Swift.String, Any>,
Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue:
"sites", intValue: nil)], debugDescription: "Expected to decode
Dictionary<String, Any> but found an array instead.", underlyingError:
nil))
I know it has to do with 'sites' returning an array (a single one) but I don't know how to fix it. Any help would be greatly appreciated.
The error message it is pretty clear you need to parse an array of objects instead of a single object.
Just change your root declaration property from
let sites: APIResponseSites
to
let sites: [APIResponseSites]
**1.** First "sites" is an array so replace
let sites: APIResponseSites
with
let sites: [APIResponseSites]()
**2.** As sites is a array collection, please print value like given below:
print(final.sites.first?.station_nm ?? "")
print(final.sites.first?.stage ?? 0.0)
Final code is here:
struct APIResponse: Codable {
let sites: [APIResponseSites]()
}
struct APIResponseSites: Codable {
let station_nm: String
let stage: Float
}
let task = URLSession.shared.dataTask(with: url, completionHandler: {
data, _, error in
guard let data = data, error == nil else {
return
}
var result: APIResponse?
do {
result = try JSONDecoder().decode(APIResponse.self, from: data)
}
catch {
print("Failed to decode with error: \(error)")
}
guard let final = result else {
return
}
print(final.sites.first?.station_nm ?? "")
print(final.sites.first?.stage ?? 0.0)
})
EDIT: Beautified JSON File
Following is the JSON File That I have hosted on local sever
{
"status": "success",
"error": "",
"response": "["{\"ip_id\":\"202\",\"ip_name\":\"P b \",\"small_desc\":\"Growth Hacker\",\"large_desc\":\"Sample description\",\"join_date\":\"\",\"vid_url\":\"https:\/\/www.theug.app\/user_videos\/pr_202.mp4\",\"img_url\":\"https:\/\/www.thnug.app\/user_thumbs\/s.jpg\",\"current_status\":\"0\",\"rate_per_hour\":\"1300\",\"currency\":\"\u20b9\"}","{\"ip_id\":\"217\",\"ip_name\":\"ss dd\",\"small_desc\":\"Talented\",\"large_desc\":\"Sample description2 \",\"join_date\":\"\",\"vid_url\":\"https:\/\/www.thug.app\/user_videos\/d.mp4\",\"img_url\":\"https:\/\/www.tsnug.app\/user_thumbs\/d.jpg\",\"current_status\":\"0\",\"rate_per_hour\":\"3850\",\"currency\":\"\u20b9\"}"]"}
I have made parsing using following code.
struct userData: Decodable {
let ip_id : Int
let ip_name : String
let small_desc : String
let large_desc : String
let join_date : String
let vid_url : String
let img_url : String
let current_status : Int
let rate_per_hour : Int
let currency : String
}
struct WebsiteDescription: Decodable {
let status: String?
let error: String?
let response: [userData]
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let jsonUrlString = "http://0.0.0.0:8000/api-response.json"
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
//perhaps check err
//also perhaps check response status 200 OK
guard let data = data else { return }
print(data)
do {
let *users* = try JSONDecoder().decode(userData.self, from: data)
print(users)
} catch let jsonErr {
print("Error serializing json:", jsonErr)
}
}.resume()
}
}
I get The error message
Error serializing json: keyNotFound(CodingKeys(stringValue: "ip_id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"ip_id\", intValue: nil) (\"ip_id\").", underlyingError: nil))
How do I Get the array data in var users?
How can I use the var users though out the scope of my class?
First of all it's mandatory to decode the root object, WebsiteDescription
let result = try JSONDecoder().decode(WebsiteDescription.self, from: data)
Second of all, you will run into another issue: The value for key response is a nested second level JSON string rather than a JSON array.
Don't do that. The JSON should look like
{"status":"success","error":"","response":[{"ip_id":"202","ip_name":"Prss Pus","small_desc":"Growth Hacker",...
Third of all, please name structs always with starting uppercase letter.
I have been having some trouble creating a temporary array of user data from Firestore. Basically I created a function that retrieves user data from a Firestore collection and then iterates through each document within that collection, creating an instance of my "Thought" struct for each one. I then append each "Thought" instance to a temporary array called "tempThoughts", and the function then returns that array. The problem is that nothing seems to be appended to the array in the function. When I test it by printing out the contents of the array upon completion, it just prints an empty array.
The data itself is being read from the Firestore collection as it prints out each document the function iterates through, so I don't think that is the problem. I also tried checking to see if I am actually creating instances of the "Thought" struct properly by printing that out, and that seemed to be working. Does anyone have any idea what's wrong with the way I am appending the struct instances to the array? Perhaps there is a better way to go about doing this? Thanks for any help in advance.
Here is my current function:
func getUserDocuments() -> [Thought]{
var tempThoughts = [Thought]()
db.collection(cUser!.uid).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
let tempThought: Thought = Thought(id: document.get("id") as! String, content: document.get("content") as! String, dateCreated: document.get("timestamp") as! String, isFavorite: (document.get("isFavorite") != nil))
tempThoughts.append(tempThought)
}
}
}
print("TEST")
print(tempThoughts)
return tempThoughts
}
Your getDocuments is an asynchronous operation. And you've updated your tempThoughts in it's completion only. But the place where you've printed it out will get executed before the getDocuments completion. Check out the order of results logged in the console.
You need to update your code like this
func getUserDocuments(_ onSuccess: ((_ thoughts: [Thought] ) -> Void), onFailuer: ((_ error: String) -> Void)) {
var tempThoughts = [Thought]()
db.collection(cUser!.uid).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
onFailuer(err)
} else {
DispatchQueue.main.async {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
let tempThought: Thought = Thought(id: document.get("id") as! String, content: document.get("content") as! String, dateCreated: document.get("timestamp") as! String, isFavorite: (document.get("isFavorite") != nil))
tempThoughts.append(tempThought)
}
print("TEST")
print(tempThoughts)
onSuccess(tempThoughts)
}
}
}
}
user this code
And you can use this function like this
getUserDocuments({ (thoughts) in
// Your logic
}) { (error) in
// error Occured
}
I ran into the following problem while trying to build something with Swift. Apparently, the values that I added into an array are not saved pass some point. They are sent just fine with the protocol while the task is running, but after it completes, if I try to see the values in the array, it returns empty.
What am i doing wrong? My guess is that it get deallocated after task finishes. If that is so, is there a way to make it strong? Is there something I should know about this task thingie? Can you please explain to me how this works and what I should do?
Here is the code:
var exchangeArray : ExchangeValues[] = [];
func fetchResult(){
var currenciesOrder = ["EUR", "USD", "GBP", "CHF", "NOK", "SEK", "DKK", "CZK","TRY", "BGN", "MDL", "PLN", "XDR", "XAU", "UAH", "RUB", "RSD","CAD", "AUD", "JPY", "EGP", "BRL","HUF", "MXN","KRW", "CNY","NZD","INR","AED", "ZAR"];
let dateFormat = NSDateFormatter();
dateFormat.dateFormat = "yyyy-MM-dd";
for days in 0..2 {
let daysToSubstract = Double(60*60*24*days);
let date : String = dateFormat.stringFromDate(NSDate().dateByAddingTimeInterval(-daysToSubstract));
var url: NSURL = NSURL(string: "http://openapi.ro/api/exchange/all.json?date=" + date);
var session = NSURLSession.sharedSession();
var task = session.dataTaskWithURL(url, completionHandler: {
(data, response, error) -> Void in
if (response != nil){
var err: NSError?;
if(err?) {
println("request Error \(err!.localizedDescription)");
}
//send the result to protocol
let results = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary;
let temp : NSDictionary = results["rate"] as NSDictionary;
for key in 0..currenciesOrder.count{
for (currencyKey : AnyObject, currencyValue : AnyObject) in temp {
if currenciesOrder[key] as String == currencyKey as String {
let tempExchange = ExchangeValues(currency: currencyKey as? String, value: currencyValue.doubleValue, date:date );
self.exchangeArray.append(tempExchange);
}
}
}
self.delegate?.didReceiveResults(self.exchangeArray);
} else {
println("error: \(error.localizedDescription)");
}
})
task.resume();
}
println("\(exchangeArray.count)");
}
I kind of figured out what the problem is:
The task block returns void, so I think it empties the array after it finishes. The result is to create another function that gets called from the task, where the array works just fine (it gets passed the values while they exist) and any further processing can be done there.
I hope this helps someone. The code is as easy as this:
func sendResults(array : ExchangeValues[]) -> Void{
println("\(exchangeArray.count)"); }
Of course, you can have the function return something if you need to.