I've got only a ViewController.swift that handles all tabs, this is the erroring code:
func parseRAM(){
guard let url = URL(string: "http://\(hostname):\(port)/STOInfo/Stats/RAM")else{
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body: [String: AnyHashable] = [
"username": username,
"password": password,
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
let task = URLSession.shared.dataTask(with: request) {data, _, error in
guard let data=data, error == nil else{
return
}
do{
let response = try JSONDecoder().decode(statsServerInfo.self, from: data)
print("SUCCESS: \(response)")
DispatchQueue.main.async{
if (self.tabBarController?.tabBar.selectedItem?.tag == 0){
self.RAMBar.progress = Float(response.out)/100
}
}
}
catch{
print(error)
}
}
task.resume()
}
@IBOutlet var CPUBar: UIProgressView!
@IBOutlet var RAMBar: UIProgressView!
The UIProgressViews are on the first tab.
The function automatically runs every 10 seconds with a timer.
The error is thrown here:
self.RAMBar.progress = Float(response.out)/100
The error happens only if I switch tabs, then I tried putting an if, to check the tab and now, with this code, it happens when, after I switch tabs, I return on the first tab.
The exact output is:
SUCCESS: statsServerInfo(ok: true, out: 4)
ServiceTOOLS_Control/ViewController.swift:307: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
2022-04-14 14:52:59.783278+0200 ServiceTOOLS Control[4837:261227] ServiceTOOLS_Control/ViewController.swift:307: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Topic:
Programming Languages
SubTopic:
Swift
Tags: