ios - Swift 3 numberOfRows table view function error -


i'm having issue trying use value return numberofrows function table view. in viewdidload have code below , prints value need, when try return same variable says found nil when unwrapping value. suggestions?

let prevorderref = ref?.child("users").child(currentuserid!).child("previous orders")      prevorderref?.observesingleevent(of: .value, with: { (snapshot) in          let dict = previousorders(snapshot: snapshot)         self.prevordercount = dict.prevordersdict?.count         print(self.prevordercount!)      }) 

update:

func tableview(_ tableview: uitableview, numberofrowsinsection section:            int) -> int {     if tableview == self.currentordertblview {          return 1     } else {          let prevorderref = ref?.child("users").child(currentuserid!).child("previous orders")          prevorderref?.observesingleevent(of: .value, with: { (snapshot) in               let dict = previousorders(snapshot: snapshot)             self.prevordercount = dict.prevordersdict?.count             print(self.prevordercount!)            })          return 2     }   } 

i have moved firebase database data observe inside numberofrows. prints value needed still unable use return.

remove ! return statement

func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int  {    if tableview == self.currentordertblview    {     return 1    }    else   {     return self.prevordercount   } } 

declare variabe as

 var prevordercount: int! 

also initialize in viewdidload as

  self.prevordercount=0 // 0 or wanted default  

these optionals assumed contain value. therefore whenever access implicitly unwrapped optional, automatically force unwrapped you. if doesn’t contain value, crash.

make sure have assigned value


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -