Post

Replies

Boosts

Views

Activity

Reply to How can I change the progress bar value when another tab is selected?
Actually, it crashes on the first print: RAMBar nil ServiceTOOLS_Control/ViewController.swift:303: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 2022-04-19 17:14:55.083701+0200 ServiceTOOLS Control[356:5920] ServiceTOOLS_Control/ViewController.swift:303: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value Testing for nil doesn't update the RAMBar but I accept it as a result because I don't mind if it updates while on another tab, the important thing is that the program doesn't crash.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’22
Reply to How can I change the progress bar value when another tab is selected?
Timers under viewDidLoad: (parseCPU the same function as parseRAM, with the URL changed)     var CPUTimer = Timer()     var RAMTimer = Timer()         self.CPUTimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true, block: { _ in self.parseCPU()         })         self.RAMTimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true, block: { _ in self.parseRAM()         }) Video of the problem: https://1drv.ms/v/s!AvW19IT_YDbihIspgUGKorL_MEURaA?e=erVeIH The class is ViewController:UIViewController
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’22
Reply to How can I change the progress bar value when another tab is selected?
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:
Apr ’22