我收到致命错误:
在解包可选值时意外发现nil
尝试将值设置为状态控制器中的任何标签或在此视图控制器中设置任何UIElement
时。
当必须设置用户界面时,应用会崩溃。
但是,日志会打印值,为什么会产生零值异常?
我的代码在
下面func polling(){
self.showActivityIndicator("Processing")
if (notificationTimer.userInfoTimer == nil){
notificationTimer.userInfoTimer = NSTimer.scheduledTimerWithTimeInterval(ApplicationConfig.pollingTimeInterval, target: self, selector: #selector(StatusController.polling), userInfo: nil, repeats: true)
}
Session.sharedInstance.ApiManager()
.request(.GET,RestApi.InfoUrl,headers:["X-Auth-Token":User.token])
.responseObject{ (response: Response<UserInfo, NSError>) in
self.indicator!.stopAnimating()
guard response.response != nil else {
log?.error("No response")
return
}
guard response.response!.statusCodeEnum.isSuccess else{
log?.error("Unable to get userInfo")
return
}
log?.debug("Response is \(response)")
User.userInfo = response.result.value
log?.debug("UserName \(User.userInfo?.userName)")
if let Details = User.userInfo?.customer?.detail{
User.detailID = Details[0].internalIdentifier!
}
guard let response = User.userInfo else{
log?.error("No response received")
return
}
self.updateUI()
}
func updateUIComponents(){
getDetails()
getTemp()
}
func getDetails(){
if (notificationTimer.DetailTimer == nil){
notificationTimer.DetailTimer = NSTimer.scheduledTimerWithTimeInterval(ApplicationConfig.pollingTimeInterval, target: self, selector: #selector(StatusController.getDetails), userInfo: nil, repeats: true)
}
let Details = User.userInfo?.customer?.details
if let Details = Details {
for details in Details {
let Status = (details.Status?.info != nil) ? details.Status?.info : ""
log?.debug("Status \(details.status?.info)")
let Charge = ((details.Capacity)!*100)/ApplicationConfig.TotalCapacity
batteryChargeLabel.text = String(Charge) + "%"
batteryChargingStatusLabel.text = Status!
}
}
}