0

I need to know how to clear the data from the object which is already assigned with some value in it.
Example

var myObject : String = "some_data"
myObject.clear() // I need to clear the information present in it.
::myObject.isInitialized // It must return false

Is there any way to do it?

Stack
  • 1,164
  • 1
  • 13
  • 26
  • Check out this answer https://stackoverflow.com/a/58337067/1117696 – Alex Timonin Jun 03 '20 at 09:37
  • 2
    It is not possible to do what you want. `isInitialized` will always return `true` as soon as `lateinit` property was initialized. For this purpose, Kotlin has `?` to mark a type as optional type. – Jenea Vranceanu Jun 03 '20 at 10:11

1 Answers1

1

You could declare variable as nullable and then assign null.

var myObject : String? = "some_data"
myObject = null // clear data

then you can reassign value to it:

myObject = "another_date"

? indicates it is nullable variable.

also you can use lateinit syntax in some purpose. Look at this topic: https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables