ios - variable used before being initialized -


hey guys im having problem this

 override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {       let destviewcontroller = segue.destinationviewcontroller as! secondtableviewcontroller       var secondarrays :  secondtabledata        if let indexpath = self.tableview.indexpathforselectedrow {         secondarrays = secondarraydata[indexpath.row]       }       destviewcontroller.secondarray = secondarrays.secondtitle   } 

im having error destviewcontroller.secondarray = secondarrays.secondtitle says: secondarrays used before being initialized, why getting error? please help, newbie swift. tia

this because of way of initialising variables in swift, in swift class each property need default value before being used.

class initialisation in swift two-phase process. in first phase, each stored property assigned initial value class introduced it. once initial state every stored property has been determined, second phase begins, , each class given opportunity customize stored properties further before new instance considered ready use.

try changing code below:

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {   let destviewcontroller = segue.destinationviewcontroller as! secondtableviewcontroller   var secondarrays :  secondtabledata?    if let indexpath = self.tableview.indexpathforselectedrow {     secondarrays = secondarraydata[indexpath.row]      //here you're making sure secondarrays must have value     destviewcontroller.secondarray = secondarrays.secondtitle   }  } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -