Can't seem to decode JSON - json

I know this type of question seems to be answered a lot but I really can't seem to make this work. I'm trying to decode some JSON data into my data structs. I think the problem is there. I may have my data model wrong, but can't quite work it out. The data is not an array, there is an array within it. Its trying to decode a dictionary into array but when I try to initialise my results variable as something other than array it won't build. I'll submit my code and the JSON data in the hopes someone can shed light!
The error I'm getting is:
JSON decode failed: Swift.DecodingError.typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
thank you so much
import SwiftUI
struct DataFormatted: Codable, Hashable {
var deliveryPoints: [DeliveryPoints]
var deliveryPointCount: Int
var postalCounty: String
var traditionalCounty: String
var town: String
var postCode: String
}
struct DeliveryPoints: Codable, Hashable {
var organisationName: String
var departmentName: String
var line1: String
var line2: String
var udprn: String
var dps: String
}
struct ContentView: View {
// I reckon the error is here:
#State private var results = [DataFormatted]()
var body: some View {
VStack{
List{
ForEach(results, id: \.self) { result in
Text(result.postalCounty)
}
}
}
.task {
await loadData()
}
}
func loadData() async {
guard let url = URL(string: "https://pcls1.craftyclicks.co.uk/json/rapidaddress?key=APIKEY&postcode=aa11aa&response=data_formatted") else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "GET"
do {
let (data, _) = try await URLSession.shared.data(for: request)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let decodedResponse = try decoder.decode([DataFormatted].self, from: data)
results = decodedResponse
} catch let jsonError as NSError {
print("JSON decode failed: \(jsonError)")
}
}
}
JSON Data:
{
"delivery_points":[
{
"organisation_name":"THE BAKERY",
"department_name":"",
"line_1":"1 HIGH STREET",
"line_2":"CRAFTY VALLEY",
"udprn":"12345678",
"dps":"1A"
},
{
"organisation_name":"FILMS R US",
"department_name":"",
"line_1":"3 HIGH STREET",
"line_2":"CRAFTY VALLEY",
"udprn":"12345679",
"dps":"1B"
}
],
"delivery_point_count":2,
"postal_county":"POSTAL COUNTY",
"traditional_county":"TRADITIONAL COUNTY",
"town":"BIG CITY",
"postcode":"AA1 1AA"
}

try something like this:
struct DataFormatted: Codable {
var deliveryPoints: [DeliveryPoint]
var deliveryPointCount: Int
var postalCounty: String
var traditionalCounty: String
var town: String
var postcode: String // <-- postcode
}
struct DeliveryPoint: Codable {
var organisationName: String
var departmentName: String
var line1: String?
var line2: String?
var line3: String?
var udprn: String
var dps: String
}
and use it like this:
let apiResponse = try decoder.decode(DataFormatted.self, from: data)
EDIT-1: here is the code I used for testing:
// -- here, default values for convenience
struct DataFormatted: Codable {
var deliveryPoints: [DeliveryPoint] = []
var deliveryPointCount: Int = 0
var postalCounty: String = ""
var traditionalCounty: String = ""
var town: String = ""
var postcode: String = "" // <-- postcode
}
struct DeliveryPoint: Hashable, Codable { // <-- here
var organisationName: String
var departmentName: String
var line1: String?
var line2: String?
var line3: String?
var udprn: String
var dps: String
}
struct ContentView: View {
#State private var results = DataFormatted()
var body: some View {
VStack{
Text(results.postalCounty)
Text(results.town)
List {
ForEach(results.deliveryPoints, id: \.self) { point in
Text(point.organisationName)
}
}
}
.task {
await loadData()
}
}
func loadData() async {
let apikey = "your-key" // <-- here
guard let url = URL(string: "https://pcls1.craftyclicks.co.uk/json/rapidaddress?key=\(apikey)&postcode=aa11aa&response=data_formatted") else {
print("Invalid URL")
return
}
do {
let (data, _) = try await URLSession.shared.data(from: url)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
results = try decoder.decode(DataFormatted.self, from: data) // <-- here
} catch {
print("JSON decode failed: \(error)")
}
}
}

Related

How to read and display a dictionary from JSON?

I am working on an app that fetches the data from JSON and displays it.
However, I am stuck with an error saying Instance method 'appendInterpolation(_:formatter:)' requires that '[String : Int]' inherit from 'NSObject'
Here is my data structure:
struct Data: Codable {
var message: String
var data: Objects
}
struct Objects: Codable {
var date: String
var day: Int
var resource: String
var stats, increase: [String: Int]
}
Function to fetch the data:
func getData() {
let urlString = "https://russianwarship.rip/api/v1/statistics/latest"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { data, _, error in
if let data = data {
do {
let decoder = JSONDecoder()
let decodedData = try decoder.decode(Data.self, from: data)
self.data = decodedData
} catch {
print("Hey there's an error: \(error.localizedDescription)")
}
}
}.resume()
}
And a ContentView with the #State property to pass the placeholder data:
struct ContentView: View {
#State var data = Data(message: "", data: Objects(date: "123", day: 123, resource: "", stats: ["123" : 1], increase: ["123" : 1]))
var body: some View {
VStack {
Button("refresh") { getData() }
Text("\(data.data.date)")
Text("\(data.data.day)")
Text(data.message)
Text("\(data.data.stats)") //error is here
Here is an example of JSON response
I wonder if the problem is in data structure, because both
Text("\(data.data.date)")
Text("\(data.data.day)")
are working just fine. If there are any workarounds with this issue – please, I would highly appreciate your help!:)
stats is [String: Int], and so when you want to use it, you need to supply the key to get the value Int, the result is an optional that you must unwrap or supply a default value in Text
So use this:
Text("\(data.data.stats["123"] ?? 0)")
And as mentioned in the comments, do not use Data for your struct name.
EDIT-1: there are two ways you can make the struct fields camelCase; one is using the CodingKeys as shown in ItemModel, or at the decoding stage, as shown in the getData() function. Note, I've also updated your models to make them easier to use.
struct DataModel: Codable {
var message: String
var data: ObjectModel
}
struct ObjectModel: Codable {
var date: String
var day: Int
var resource: String
var stats: ItemModel
var increase: ItemModel
}
struct ItemModel: Codable {
var personnelUnits: Int
var tanks: Int
var armouredFightingVehicles: Int
// ...
// manual CodingKeys
// enum CodingKeys: String, CodingKey {
// case tanks
// case personnelUnits = "personnel_units"
// case armouredFightingVehicles = "armoured_fighting_vehicles"
// }
}
struct ContentView: View {
#State var dataModel = DataModel(message: "", data: ObjectModel(date: "123", day: 123, resource: "", stats: ItemModel(personnelUnits: 123, tanks: 456, armouredFightingVehicles: 789), increase: ItemModel(personnelUnits: 3, tanks: 4, armouredFightingVehicles: 5)))
var body: some View {
VStack {
Button("get data from Server") { getData() }
Text("\(dataModel.data.date)")
Text("\(dataModel.data.day)")
Text(dataModel.message)
Text("\(dataModel.data.stats.armouredFightingVehicles)") // <-- here
}
}
func getData() {
let urlString = "https://russianwarship.rip/api/v1/statistics/latest"
if let url = URL(string: urlString) {
URLSession.shared.dataTask(with: url) { data, _, error in
if let data = data {
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase // <-- here
dataModel = try decoder.decode(DataModel.self, from: data)
} catch {
print("--> error: \(error)")
}
}
}.resume()
}
}
}

JSON Object to string Swift UI

import SwiftUI
extension String{
func load() ->UIImage {
do{
//string to URL
guard let url = URL(string: self) else{
//empty image if wrong url
return UIImage()
}
//url to data
let data : Data = try Data(contentsOf: url)
//create uuimage obj from data
return UIImage(data: data) ?? UIImage()
}catch{
}
return UIImage()
}
}
struct ContentView: View {
#State private var quoteData : QuoteData?
var body: some View {
ZStack{
VStack{
Text(quoteData?.lat ?? "Latitude")
Text(quoteData?.long ?? "Longitude")
//------------------------HELP HERE
Image(uiImage: "https://maps.googleapis.com/maps/api/staticmap?center=LATITUDE,LONGITUDE&zoom=16&size=400x400&key=AIzaSyClGOTMpV2kKF27bxAo6nm3pIq7zmW69Fw".load())
struct QuoteData: Decodable{
//var id: Int
var long: String
var lat: String
var apiURL: String
}
I want to insert the long and lat variables of QuoteData inside the url string that is used in IMAGE(uimage
I cant find an answer , been looking for hours with no luck
this is just a fragment of the code it will be nice if someone can help me .
try something like this, using \(lat),\(lon) "...to insert the long and lat variables of QuoteData inside the url string...":
struct ContentView: View {
#State private var quoteData: QuoteData? = QuoteData(long: "139.7514", lat: "35.685", apiURL: "") // for testing, Tokyo
var body: some View {
VStack{
Text(quoteData?.lat ?? "Latitude")
Text(quoteData?.long ?? "Longitude")
if let lat = quoteData?.lat, let lon = quoteData?.long {
Image(uiImage: "https://maps.googleapis.com/maps/api/staticmap?center=\(lat),\(lon)&zoom=16&size=400x400&key=AIzaSyClGOTMpV2kKF27bxAo6nm3pIq7zmW69Fw".load())
}
}
}
}
PS: this has nothing to do with JSON object to String

Why is my API call not completely reached?

I am currently building a public transportation app in SwiftUI Xcode 12 beta, where you input origin, destination, time of arrival, and date. The idea is to then convert the origin and destination input (private func FetchOriginCoordinates and private func FetchDestCoordinates) into coordinates using an API. This API will then give that data to another API that plans the route.
The problem is that the route planner API doesn't give any output. I have tried printing self.Trips.append(...) but the list is completely empty. I believe this is because the code doesn't reach that part of the code. I have put some print statements in the code in order to understand where the error is occurring. When I call the API my output in the console is this:
Test 2021-04-16 22:33:19.271246+0200 OnTrack2.0[6592:428359] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed Optional(http://api.sl.se/api2/TravelplannerV3_1/trip.json?key=40892db48b394d3a86b2439f9f3800fd&originExtId=300101115&destExtId=300101426&Date=2021-04-18&Time=09:20&searchForArrival=1) [] 10767 bytes Level 1 Level 1.5
This means that the code only reaches the print("Level 1.5") statement. But never the following print statements such as print("Level 2") or print(self.trips). Why is that and how do I solve it?
import Foundation
struct NominationStructure: Decodable {
var lat: String
var lon: String
enum CodingKeys: String, CodingKey {
case lat = "lat"
case lon = "lon"
}
}
struct NearbyStopsStructure: Decodable {
var stopLocationOrCoordLocation: [stopLocationOrCoordLocationStructure]
}
struct stopLocationOrCoordLocationStructure: Decodable {
var StopLocation: StopLocationStructure
}
struct StopLocationStructure: Decodable {
var mainMastExtId: String
}
struct JSONStructure: Decodable {
var Trip: [TripStructure]
}
struct TripStructure: Decodable {
var LegList: LegListStructure
}
struct LegListStructure: Decodable {
var Leg: [LegStructure]
}
struct LegStructure: Decodable {
var Origin: StationStructure
var Destination: StationStructure
var Product: ProductStructure
var name: String
var type: String
var dist: String
}
struct StationStructure: Decodable {
var time: String
var name: String
var date: String
}
struct ProductStructure: Decodable {
var catIn: String
}
struct LocationInfo {
var iD = String()
var input = String()
var lat = String()
var lon = String()
var name = String()
var time = String()
var date = String()
var vehicleType = String()
var transportType = String()
var dist = String()
var legName = String()
}
//One way is to call your functions when you know a step is completed
enum TripFetchStatus: String {
case start
case fetchedOriginCoordinates
case fetchedOriginId
case fetchedDestinationCoordinates
case fetchedDestinationId
case fetchingTrip
case done
case stopped
}
class PlanTripViewModel: ObservableObject {
//Apple frowns upon frezzing a screen so having a way to update the user on what is going on
//When a step is complete have it do something else
#Published var fetchStatus: TripFetchStatus = .stopped{
didSet{
switch fetchStatus {
case .start:
print("Test")
FetchOriginCoordinates { (cors) in
self.origin.lat = cors[0].lat
self.origin.lon = cors[0].lon
self.fetchStatus = .fetchedOriginCoordinates
}
case .fetchedOriginCoordinates:
self.FetchOriginID { (stops) in
self.origin.iD = stops.stopLocationOrCoordLocation[0].StopLocation.mainMastExtId
self.fetchStatus = .fetchedOriginId
}
case .fetchedOriginId:
FetchDestCoordinates { (cors) in
self.dest.lat = cors[0].lat
self.dest.lon = cors[0].lon
self.fetchStatus = .fetchedDestinationCoordinates
}
case .fetchedDestinationCoordinates:
self.FetchDestID { (stops) in
self.dest.iD = stops.stopLocationOrCoordLocation[0].StopLocation.mainMastExtId
self.fetchStatus = .fetchedDestinationId
}
case .fetchedDestinationId:
//Once you have everthing in place then go to the next API
FetchTrip()
case .fetchingTrip:
print("almost done")
case .done:
print("any other code you need to do")
case .stopped:
print("just a filler")
}
}
}
#Published var trip: LocationInfo = LocationInfo()
#Published var dest: LocationInfo = LocationInfo()
#Published var origin: LocationInfo = LocationInfo()
#Published var arrivalTime = String()
#Published var travelDate = String()
#Published var tripIndex = Int()
#Published var Trips: [Dictionary<String, String>] = []
public func FetchTrip() {
Trips.removeAll()
let tripKey = "40892db48b394d3a86b2439f9f3800fd"
let tripUrl = URL(string: "http://api.sl.se/api2/TravelplannerV3_1/trip.json?key=\(tripKey)&originExtId=\(self.origin.iD)&destExtId=\(self.dest.iD)&Date=\(self.travelDate)&Time=\(self.arrivalTime)&searchForArrival=1")
print(tripUrl)
URLSession.shared.dataTask(with: tripUrl!) {data, response, error in
if let data = data {
print(data)
print("Level 1")
do {
print("Level 1.5")
if let decodedJson = try? JSONDecoder().decode(JSONStructure.self, from: data) {
self.tripIndex = decodedJson.Trip.count - 1
print("Level 2")
for i in 0..<decodedJson.Trip[self.tripIndex].LegList.Leg.count {
self.trip.transportType = decodedJson.Trip[self.tripIndex].LegList.Leg[i].type
if self.trip.transportType == "WALK" {
self.origin.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.name
self.origin.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.time.prefix(5))
self.origin.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.date
self.dest.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.name
self.dest.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.time.prefix(5))
self.dest.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.date
self.trip.vehicleType = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Product.catIn
self.trip.dist = decodedJson.Trip[self.tripIndex].LegList.Leg[i].dist
print(self.origin.name)
print(self.dest.name)
self.Trips.append(["Origin": self.origin.name, "Destination": self.dest.name, "OriginTime": self.origin.time, "DestTime": self.dest.time, "OriginDate": self.origin.date, "DestDate": self.dest.date, "TransportType": self.trip.transportType, "VehicleType": self.trip.vehicleType, "Distance": self.trip.dist])
}
else {
self.origin.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.name
self.origin.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.time.prefix(5))
self.origin.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Origin.date
self.dest.name = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.name
self.dest.time = String(decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.time.prefix(5))
self.dest.date = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Destination.date
self.trip.vehicleType = decodedJson.Trip[self.tripIndex].LegList.Leg[i].Product.catIn
self.trip.legName = decodedJson.Trip[self.tripIndex].LegList.Leg[i].name
print(self.origin.name)
print(self.dest.name)
self.Trips.append(["Origin": self.origin.name, "Destination": self.dest.name, "OriginTime": self.origin.time, "DestTime": self.dest.time, "OriginDate": self.origin.date, "DestDate": self.dest.date, "TransportType": self.trip.transportType, "VehicleType": self.trip.vehicleType, "LegName": self.trip.legName])
}
}
}
} catch {
print(error)
}
}
}.resume()
print(self.Trips)
}
//Simple version just to replicate put your code within
private func FetchOriginCoordinates(completion: #escaping ([NominationStructure]) -> ()) {
let scheme = "https"
let host = "nominatim.openstreetmap.org"
let path = "/search"
let queryItemCountry = URLQueryItem(name: "country", value: "Sweden")
let queryItemCity = URLQueryItem(name: "city", value: "Stockholm")
let queryItemStreet = URLQueryItem(name: "street", value: self.origin.input)
let queryItemFormat = URLQueryItem(name: "format", value: "json")
var urlComponents = URLComponents()
urlComponents.scheme = scheme
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = [queryItemCountry, queryItemCity, queryItemStreet, queryItemFormat]
URLSession.shared.dataTask(with: urlComponents.url!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode([NominationStructure].self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
private func FetchDestCoordinates(completion: #escaping ([NominationStructure]) -> ()) {
let scheme = "https"
let host = "nominatim.openstreetmap.org"
let path = "/search"
let queryItemCountry = URLQueryItem(name: "country", value: "Sweden")
let queryItemCity = URLQueryItem(name: "city", value: "Stockholm")
let queryItemStreet = URLQueryItem(name: "street", value: self.dest.input)
let queryItemFormat = URLQueryItem(name: "format", value: "json")
var urlComponents = URLComponents()
urlComponents.scheme = scheme
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = [queryItemCountry, queryItemCity, queryItemStreet, queryItemFormat]
URLSession.shared.dataTask(with: urlComponents.url!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode([NominationStructure].self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
private func FetchOriginID(completion: #escaping (NearbyStopsStructure) -> ()) {
let nearbyStopsKey = "8444f9a2f75f4c27937a7165abd532a0"
let stopIDUrl = URL(string: "http://api.sl.se/api2/nearbystopsv2.json?key=\(nearbyStopsKey)&originCoordLat=\(self.origin.lat)&originCoordLong=\(self.origin.lon)&maxNo=1")
URLSession.shared.dataTask(with: stopIDUrl!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode(NearbyStopsStructure.self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
private func FetchDestID(completion: #escaping (NearbyStopsStructure) -> ()) {
let nearbyStopsKey = "8444f9a2f75f4c27937a7165abd532a0"
let stopIDUrl = URL(string: "http://api.sl.se/api2/nearbystopsv2.json?key=\(nearbyStopsKey)&originCoordLat=\(self.dest.lat)&originCoordLong=\(self.dest.lon)&maxNo=1")
URLSession.shared.dataTask(with: stopIDUrl!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode(NearbyStopsStructure.self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume()
}
}

Swift: key not found JSON decoding

I am trying to decode a JSON object, from this website: https://www.thesportsdb.com/api/v1/json/1/search_all_leagues.php?c=France&s=Soccer
I would like to store them in an array of Soccer elements, and show them in cells.
This is the code I did, but I have key not found errors, how is that possible?
class Soccer: Codable {
var strLeague: String
var strDescriptionEN: String
var strBadge: String
var strDivision: String
var intFormedYear: String
var strCountry: String
init(strLeague: String, strDescriptionEN: String, strBadge: String, strDivision: String, intFormedYear: String, strCountry: String) {
self.strLeague = strLeague
self.strDescriptionEN = strDescriptionEN
self.strBadge = strBadge
self.strDivision = strDivision
self.intFormedYear = intFormedYear
self.strCountry = strCountry
}
}
class SoccerTVC: UITableViewController {
var leagues = [Soccer]()
func download(at url: String, handler: #escaping (Data?) -> Void)
{
// 1 - Create URL
guard let url = URL(string: url) else {
debugPrint("Failed to create URL")
handler(nil)
return
}
// 2 - Create GET Request
var request: URLRequest = URLRequest(url: url)
request.httpMethod = "GET"
// 3 - Create download task, handler will be called when request ended
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in handler(data)
}
task.resume()
}
func getSoccer() {
// 1 - Download Soccer
download(at: "https://www.thesportsdb.com/api/v1/json/1/search_all_leagues.php?c=France&s=Soccer")
{ (SoccerData) in
if let Soccerdata = SoccerData {
// 2 - Decode JSON into a array of Game object
let decoder: JSONDecoder = JSONDecoder()
do {
let jsonData = [try decoder.decode(Soccer.self, from: Soccerdata)]
self.leagues = jsonData
debugPrint(self.leagues)
DispatchQueue.main.sync {
self.tableView.reloadData()
}
}
catch {
debugPrint("Failed to parse data - error: \(error)")
}
}
else
{
debugPrint("Failed to get soccer data")
}
}
}
override func viewDidLoad() {
getSoccer()
super.viewDidLoad()
}
}
Error message:
Failed to parse data - error: keyNotFound(CodingKeys(stringValue:
"strLeague", intValue: nil), Swift.DecodingError.Context(codingPath:
[], debugDescription: "No value associated with key
CodingKeys(stringValue: \"strLeague\", intValue: nil)
(\"strLeague\").", underlyingError: nil))
Your desired values is in country array key so you need to change your struct with :
struct Soccer: Codable {
let countrys : [Country]?
}
struct Country : Codable{
var strLeague: String
var strDescriptionEN: String
var strBadge: String
....
}
And you need to change your json decoder like :
var leagues = [Country]()
let jsonData = try decoder.decode(Soccer.self, from: Soccerdata)
self.leagues = jsonData.countrys
}
Try this instead:
let jsonData = try JSONDecoder().decode([String:[Soccer]].self, from: Soccerdata)
self.leagues = jsonData.values

How do you display JSON Data in swiftUI

I have problem with showing JSON Data in SwiftUI, I get the data from Genius API I currently search for song and can confirm that I get the data extracted correctly; example I can print out the title of the result:
This is how I fetch the data
class NetworkManager: ObservableObject {
var objectWillChange = PassthroughSubject<NetworkManager, Never>()
var fetchedSongsResults = [hits]() {
willSet {
objectWillChange.send(self)
}
}
init() {
fetchSongs()
}
func fetchSongs() {
guard let url = URL(string: "https://api.genius.com/search?q=Sia") else { return }
var urlRequest = URLRequest(url: url)
urlRequest.setValue("Bearer TOKEN", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: urlRequest) {data, response, error in
guard let data = data else { return }
//print(String(decoding: data, as: UTF8.self))
let songs = try! JSONDecoder().decode(feed.self, from: data)
DispatchQueue.main.async {
self.fetchedSongsResults = songs.response.hits
}
}.resume()
}
}
So when I get the data I save to the variable fetchedSongsResults and this seems correctly but for what ever reason when I try to print the count for example it says that i empty and also I can't loop through the fetchedSongsResults using a list or ForEach this is how, (which I believe s because I have not made the model identifiable) I tried to print the count of fetchedSongsResults,
This initialized outside the body (just so you know)
#State var networkManager = NetworkManager()
This is inside the body
Text("\(networkManager.fetchedSongsResults.count)")
If your are wondering how my structure looks like when I decode the JSON Data then here it is
struct feed: Codable {
var meta: meta
var response: response
}
struct meta: Codable {
var status: Int
}
struct response: Codable {
var hits: [hits]
}
struct hits: Codable {
var index: String
var type: String
var result: song
}
struct song: Codable, Identifiable {
var id: Int
var header_image_thumbnail_url: String
var url: String
var title: String
var lyrics_state: String
var primary_artist: artist
}
struct artist: Codable {
var name: String
}
Try: #ObservedObject var networkManager = NetworkManager().