ios - "unexpectedly found nil" - delegate assignment in subVC -


i have been stuck on past few days -

graphview ui class instance - declare calculatorviewdatasource protocol outside of graphview class, contain weak var calcdatasource: calculatorviewdatasource? in it.

in graphviewcontroller, in didset{} of graphview outlet try set delegate assume existing calculatorviewcontroller using following code:

 if let vc = splitviewcontroller?.viewcontrollers {             println(vc)             println(vc.count)             if let fst = vc.first {                 println(fst.subviews)                 println(fst.subviews.first)             }         } graphview.calcdatasource = splitviewcontroller!.viewcontrollers.first!.subviews.first calculatorviewcontroller 

the code compile, crash "unexpectedly found nil" when assignment of graphview.calcdatasource

(output)

[<uinavigationcontroller: 0x7b71bd10>] 1 nil fatal error: unexpectedly found nil while unwrapping optional value

(storyboard)

splitviewcontroller - navigationcontroller - (master) calculatorviewcontroller      navigationcontroller - (detail) graphviewcontroller` 

========

solved!

i worked around issue working inside prepareforsegue (with storyboard segue on button called "show graph")

//calculatorviewcontroller.swift: override func prepareforsegue(segue: uistoryboardsegue?, sender: anyobject!) {     if (segue!.identifier == "show graph") {         var yournextviewcontroller = (segue!.destinationviewcontroller uinavigationcontroller)         var detail = yournextviewcontroller.viewcontrollers[0] graphviewcontroller         var tempview = detail.view // forces view object existence, without compile, next line crash @ runtime (graphview nil)         detail.graphview.calcdatasource = self     } } 

note var tempview = detail.view critical here, despite not being used. understand setting view , outlet..

how (safely) unwrap optionals:

graphview.calcdatasource = splitviewcontroller!.viewcontrollers.first!.subviews.first calculatorviewcontroller  if let vc = splitviewcontroller?.viewcontrollers {     println(vc)     if let fst = vc.first {         println(fst.subviews)         println(fst.subviews.first)     } } 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -