The Database Hub | Back4App
The Database Hub | Back4App
How it works
  • Log In
  • Sign Up

  1. Back4App
  2. World Continents, Countries, and Cities
Public

1210
5637
1210
5637
  • DatabaseDatabase
  • GraphQL API PlaygroundAPI Playground
  • Get Started GuideGet Started
  • IssuesIssues
  • ContributorsContributors
  • Database
    Database
    GraphQL API Playground
    API Playground
    Get Started Guide
    Get Started
    Contributors
    Contributors

  1. Closed IssueClosed
  2. Xcode playground keeps finding decoding error

New IssueNew

andysappdesign commented this issue on Sep 2020

I am trying to implement the Countries API into my app, but I am testing this first to get it correct in playground but keep getting the error:

valueNotFound(__lldb_expr_109.Country, Swift.DecodingError.Context(codingPath: CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), _JSONKey(stringValue: "Index 0", intValue: 0), debugDescription: "Expected Country but found null instead.", underlyingError: nil))

The code for this is: import UIKit import PlaygroundSupport import SwiftUI import Foundation

// MARK: Country

struct Country: Hashable, Codable { var objectId: String var code: String var name: String var native: String var phone: String var capital: String var currency: String var emojiU: String var createdAt: String var updatedAt: String var geonameid: Int var languages: Languages var cities: CountryCities var timezones: Timezones var provinces: Proviences

}

struct Responce: Hashable, Codable { var results: Country }

// MARK: Languages

struct Languages: Hashable, Codable { var __type: String var className: String }

// MARK: CountryCities

struct CountryCities: Hashable, Codable { var __type: String var className: String }

// MARK: Timezones

struct Timezones: Hashable, Codable { var __type: String var className: String }

// MARK: Proviences

struct Proviences: Hashable, Codable { var __type: String var className: String }

var id = "TRTRnWFQ48yxQFImnzZB1fZ0Np9Ex8g4GlFlcQWG" var key = "hGgWjAMWkdJYu2lavC9yTitWavETce0qwkXXxwkM"

func countryAPIRequest() { let filter = """ { "name": { "$exists": true } } """.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) let url = URL(string: "https://parseapi.back4app.com/classes/Continentscountriescities_Country?limit=10&excludeKeys=emoji,continent,shape&where=(filter!)")! var urlRequest = URLRequest(url: url) urlRequest.setValue(id, forHTTPHeaderField: "X-Parse-Application-Id") // This is your app's application id urlRequest.setValue(key, forHTTPHeaderField: "X-Parse-REST-API-Key") // This is your app's REST API key URLSession.shared.dataTask(with: urlRequest, completionHandler: { (data, response, error) in

    if let data = data {
        do {
            let res = try JSONDecoder().decode(Responce.self, from: data)
            print(res)
        } catch let error {
            print(data)
            print(error)
        }
    }
}).resume()

}

countryAPIRequest()

alex commented this issue on Sep 2020

Hey @andysappdesign! Thank you for joining our Database Hub and bringing this up. May I ask why are you retrieving data like that and not using the Parse SDK? It makes things soooo much easier!

andysappdesign commented this issue on Sep 2020

Hello! Thank you for the response! Truth be told, this is my first proper project and still got ALOT to learn! I will give Parse SDK a go! Thank you

Closed Issueandysappdesign closed this issue on Sep 2020
Please enter a comment for this issue.