Had to make this an answer since comments don't seem to like my Code snippets :(
Got a number of errors :(.
Thank you so much for helping me with this, once again. This is so helpful - I don't know what I would've done.
//Here are the errors I received..
//
//Example of how I formatted this...
// Line xx: {code that triggered error} → Error Message
//
//
//Line 26: {myData = readData} → Couldn't find myData in Scope
//Line 38: {print("Decode error", jsonString} → Cannot find 'jsonString' in scope
//Line 46: {Text(myVar?.value1 ?? "")} → Cannot find 'myVar' in scope
//Here's the code...
import SwiftUI
import Foundation
struct AllVar: Codable {
public var value1: String
public var value2: String
public var value3: String
}
func decode() -> AllVar? {
var myVar : AllVar?
var data : Data?
// SKIP THIS let data = jsonString.data(using: .utf8, allowLossyConversion: false) // Use directly the data you get from url
if let url = URL(string: "https://sheetsu.com/apis/v1.0su/4a6eb0e90aa4") { // urlString is the url
let urlSession = URLSession(configuration: .default).dataTask(with: url) { (readData, response, error) in
myData = readData
}
urlSession.resume()
}
let jsonDecoder = JSONDecoder()
do {
myVar = try jsonDecoder.decode(AllVar.self, from: data!)
return myVar
} catch {
print("Decode error", jsonString)
}
return nil
}
struct ContentView: View {
var body: some View {
VStack{
Text(myVar?.value1 ?? "")
Text(myVar?.value2 ?? "")
Text(myVar?.value3 ?? "")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}