I have a class that looks like this (simplified):
class GuideViewController: UIViewController, StoreSubscriber {
var tileRenderer: MKTileOverlayRenderer! // <------ this needs to be set by whoever instantiates this class
override func viewDidLoad() {
super.viewDidLoad()
...
}
}
My app uses this GuideViewController class to display many different styles of maps, so the tileRenderer instance variable can have many different values.
I want a compile-time guarantee that tileRenderer will never be nil, instead of using an implicitly-unwrapped optional.
How can I achieve this?
Things I've considered so far but am unsure about
- Setting
tileRendererin theinit()method of the GuideViewController. This was my first instinct by this answer implies that this is not possible, or an antipattern. - Setting
tileRendererinviewDidLoad(). This seems to require using an implicitly unwrapped optional which bypasses compile-time checks. Also, I'm under the impression thatviewDidLoad()is only called once for the view controller in the lifecycle of the app Manually setting
tileRendererafter instantiating the VC. E.g.,let vc = self.storyboard?.instantiateViewController(withIdentifier: "GuideViewController") vc.tileRenderer = MKTileOverlayRenderer(...) // <----- can I make the compiler force me to write this line? navigationController?.pushViewController(vc!, animated: true)
Forgive me for asking such a naive question—I'm fairly new to iOS development.