Tôi tự hỏi nếu có một cách để kiểm tra xem một biến số lateinit
đã được khởi tạo chưa.Cách kiểm tra xem biến "lateinit" đã được khởi tạo chưa?
import javafx.application.Application
import javafx.event.EventHandler
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Scene
import javafx.scene.control.Button
import javafx.scene.control.ComboBox
import javafx.scene.layout.VBox
import javafx.stage.DirectoryChooser
import javafx.stage.Stage
import java.io.File
class SeriesManager() {
lateinit var seriesDir: File
val allSeries by lazy {
seriesDir.listFiles().map { it.name }.toTypedArray()
}
}
class SeriesManagerUI : Application() {
override fun start(primaryStage: Stage) {
val sm = SeriesManager()
val setSeriesDirBtn = Button("Change all series location").apply {
onAction = EventHandler {
sm.seriesDir = DirectoryChooser().apply {
title = "Choose all series location"
}.showDialog(primaryStage)
}
}
val allSeriesList = ComboBox<String>().apply {
promptText = "Select a series from here"
isDisable = // I want this to be always true, unless the SeriesManager.seriesDir has been initialized
}
val setCurrentEpisodeBtn = Button("Change the current episode")
val openNextEpisode = Button("Watch the next episode")
val layout = VBox(
setSeriesDirBtn,
allSeriesList,
setCurrentEpisodeBtn,
openNextEpisode
).apply {
padding = Insets(15.0)
spacing = 10.0
alignment = Pos.CENTER
}
primaryStage.apply {
scene = Scene(layout).apply {
minWidth = 300.0
isResizable = false
}
title = "Series Manager"
}.show()
}
}
fun main(args: Array<String>) {
Application.launch(SeriesManagerUI::class.java, *args)
}
Có lẽ những gì bạn cần là làm cho nullable tài sản (thay đổi loại để file ''?) Và chỉ cần kiểm tra nếu nó là null thay vì ? –
Vâng, tôi thực sự đã thử điều đó và nó sẽ thực hiện thủ thuật, tuy nhiên tôi sẽ phải chỉnh sửa 'allSeries' var thành' seriesDir? .listFiles() ?. map {it.name} ?. toTypedArray() ', đó là không phải là rất "đẹp" –
Bạn có thể làm một kiểm tra null đồng bằng cũ và đúc thông minh sẽ làm cho nó đẹp hơn. 'if (seriesDir! = Null) {' 'allSeries = seriesDir.listFiles(). Map {it.name} .toTypedArray()' '}' –