Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Non-optional stored properties for UIViewController Custom Subclass

When subclassing UIViewController ( and other similar classes ) we do not have to deal with any initialization, so if I create a stored property that is not an optional, Xcode will complain that there are no initializer.

So how can I create a non-optional store property in these classes? or should I?

like image 392
harinsa Avatar asked Feb 02 '26 16:02

harinsa


1 Answers

You have 2 options:

  • initialize it inline

    var myProp = MyClass()
    
  • declare it as implicitly unwrapped optional:

    var myProp: MyClass!
    

    and initialize it anywhere in the view controller - usually in viewDidLoad. This is the pattern commonly used for outlets, and one of the few cases where I tolerate implicitly unwrapped

like image 164
Antonio Avatar answered Feb 04 '26 07:02

Antonio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!