Post

Replies

Boosts

Views

Activity

Reply to Basic API into app to variables
Got thrown a fatal error in the do catch block under let jsonDecoder ... 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             data = readData          }          urlSession.resume()      }      let jsonDecoder = JSONDecoder()      do {          myVar = try jsonDecoder.decode(AllVar.self, from: data!) //Fatal Error Thrown here → Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value          return myVar      } catch {          //print("Decode error", jsonString)      }      return nil  }  struct ContentView: View {      var myVar = decode() //Line 40 Added Code      var body: some View {          VStack{              Text(myVar?.value1 ?? "")              Text(myVar?.value2 ?? "")              Text(myVar?.value3 ?? "")          }      }  }  struct ContentView_Previews: PreviewProvider {      static var previews: some View {          ContentView()      } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Basic API into app to variables
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()     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Basic API into app to variables
I, in no way intended to promote that website and am sorry if I gave off the wrong impression. Your point is completely valid, and I was unaware of it. Sorry for the confusion. I removed the URL to the API and replaced it with the contents of the API instead.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22