我正在嘗試將字串值分配給資料類物件的字串屬性部分,但它不起作用:
appointmentInfo.mySymptoms?.medsDescription = "string description"
在哪里,
data class AppointmentInfo(var id : Int? = null,
var planType : String? = null,
var dateFormat : DateFormat,
var mySymptoms : MySymptoms? = null) : Parcelable
@Parcelize
data class MySymptoms(var grantAccess : Boolean,
var takingMeds : Boolean,
var medsDescription : String? = null,
var symptomList : List<String>? = null,
var symptomsDescription : String? = null,
var generalDescription : String? = null ```
**appointmentInfo.mySymptoms?.medsDescription is always null**
uj5u.com熱心網友回復:
確保正確分配變數。
代碼
data class AppointmentInfo(
var mySymptoms : MySymptoms? = null,
)
data class MySymptoms(
var medsDescription : String? = null,
)
fun main() {
var appointmentInfo = AppointmentInfo()
appointmentInfo.mySymptoms = MySymptoms()
appointmentInfo.mySymptoms?.medsDescription = "string description"
println(appointmentInfo.mySymptoms?.medsDescription)
}
輸出
string description
Kotlin Playground 鏈接
uj5u.com熱心網友回復:
您應該使用copy()資料類的功能https://kotlinlang.org/docs/data-classes.html#copying因為 Jet Brains 建議所有資料類成員都應該是只讀的val
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/479898.html
標籤:科特林
下一篇:為什么呼叫KotlinCoreEnvironment.createForProduction會導致IlegalStateException?
