I don't know why there's nil on the JSON structure. Below is the code. The structure is relatively simple, but somehow I just can't parse the decoded JSON.
ContentView.swift
import SwiftUI
var fetchTotal = getTotalValue()
struct ContentView: View {
var body: some View {
Text(fetchTotal)
//Text("gg")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Decoded_structure.swift:
import Foundation
import SwiftUI
struct PostTotal: Codable {
var rows: [Layer0]
struct Layer0: Codable, Hashable {
var totalmarketvalue: String
var totaltodaysprofit: String
var todaysprofitpercent: String
var totalasset: String
var maxpurchasepower: String
}
}
let webstringtotal: String = "http://gsx2json.com/api?id=1z9WvS25hDvLaNPX1HwTE-CiJ46UvmqlUWT-iWVT0aJY&sheet=1&integers=false&column=false"
func getTotalValue() -> String {
let urlTotal = URL(string: webstringtotal)
var results: String = ""
URLSession.shared.dataTask(with: urlTotal!) { (datas, _, _) in
let postss = try! JSONDecoder().decode([PostTotal.Layer0].self, from: datas!)
//let layer0 = [postss.rows]
results = postss[0].totalasset
}
.resume()
return results
}
And the error message:
2020-06-11 15:51:46.211463+0800 PureTestProject[28884:988078] [Agent] Received remote injection
2020-06-11 15:51:46.211753+0800 PureTestProject[28884:988078] [Agent] Create remote injection Mach transport: 600001500770
2020-06-11 15:51:46.212150+0800 PureTestProject[28884:988022] [Agent] No global connection handler, using shared user agent
2020-06-11 15:51:46.212339+0800 PureTestProject[28884:988022] [Agent] Received connection, creating agent
2020-06-11 15:51:46.897118+0800 PureTestProject[28884:988022] [Agent] Received message: < DTXMessage 0x600001a150e0 : i2.0e c0 object:(__NSDictionaryI*) {
"products" : <NSArray 0x600003c141b0 | 1 objects>
"id" : [0]
"scaleFactorHint" : [3]
"providerName" : "15PureTestProject20ContentView_PreviewsV"
"updates" : <NSArray 0x7fff8062d430 | 0 objects>
} > {
"serviceCommand" : "forwardMessage"
"type" : "display"
}
2020-06-11 15:51:46.938191+0800 PureTestProject[28884:988035] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2020-06-11 15:51:46.938349+0800 PureTestProject[28884:988035] Cannot start load of Task <EF8F8A3E-E1FC-4E80-808A-1B576BBCDBD6>.<1> since it does not conform to ATS policy
2020-06-11 15:51:46.940051+0800 PureTestProject[28884:988033] Task <EF8F8A3E-E1FC-4E80-808A-1B576BBCDBD6>.<1> finished with error [-1022] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x6000030045d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://gsx2json.com/api?id=1z9WvS25hDvLaNPX1HwTE-CiJ46UvmqlUWT-iWVT0aJY&sheet=1&integers=false&column=false, NSErrorFailingURLKey=http://gsx2json.com/api?id=1z9WvS25hDvLaNPX1HwTE-CiJ46UvmqlUWT-iWVT0aJY&sheet=1&integers=false&column=false, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/benny/temp_files/test_xcode_11/PureTestProject/PureTestProject/Decoded_struct.swift, line 31
2020-06-11 15:51:46.957521+0800 PureTestProject[28884:988033] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/benny/temp_files/test_xcode_11/PureTestProject/PureTestProject/Decoded_struct.swift, line 31
(lldb)