Search code examples
kotlinoption-typeunwrap

Does Kotlin have an equivalent to Implicitly Unwrapped Optionals in Swift?


Implicitly unwrapped optionals are a useful feature of Swift for things like UI elements that are not assigned during a class's constructor, but can be safely be assumed to be non-null for the majority of functions (as they will have been assigned in a viewDidLoad).

eg.

@IBOutlet weak var textView : UITextView! 

Is there an equivalent for Kotlin, or a workaround that achieves the same effect?


Solution

  • lateinit var may be a suitable solution. Kotlin just assumes they are not null. https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties