ios - How To Set Total1 And Total2 Value Into Label When User Scrolls on Both Segment Of picker View (Dynamically)? -


func pickerview(_ pickerview: uipickerview, didselectrow row: int, incomponent component: int) {      if component == 0 {         lbl1.text = pdata[0][row]         lbl1.adjustsfontsizetofitwidth = true         img1.image = uiimage(named : pdata[0][row])         price1.text = pricedata[row]         total1 = int(pricedata[row]) ?? 0          }   else if component == 1 {         lbl2.text = pdata[1][row]         lbl2.adjustsfontsizetofitwidth = true         img2.image = uiimage(named : imgdata[row])         price2.text = pricedata2[row]         total2 = int(pricedata2[row]) ?? 0      }     totalprice.text = string(total1! + total2!)    } 

when user scroll on 1 segment total becomes nil , app crashes how can store both total 1 , total 2 variable show sum of price user

you can use coalescing operator prevent force unwrap:

totalprice.text = string((total1 ?? 0) + (total2 ?? 0)) 

https://en.wikipedia.org/wiki/null_coalescing_operator


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -