if条件在kotlin中的数据类构造函数内

时间:2018-05-02 13:55:15

标签: kotlin

我有data class,我正在创建object。我传递的参数值如下:

val leadDetails = AddLeadDetails(AgentId = agent.UserId,
                Name = leadUserName.text.toString().trim(),
                MobileNo = leadMobileNumber.text.toString().trim(),
                ProductType = productTypeId,
                LoanType = productTypeItem,
                ApplicationStatus = //if condition to put value i.e if(string == "s") "One value" else "Second Value"
                Amount = productAmountText.text.toString().trim(),
                Pincode = pinCodeText.text.toString().trim(),
                Remarks = customerRemarks.text.toString().trim(),
                Type = referType!!)

我想根据ApplicationStatus条件向if添加值。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

这很简单:

fun main(args: Array<String>) {
    TestClass(if (currentTimeMillis() % 2 == 0L) "x" else "y")
}

data class TestClass(val text: String)

Kotlin中有no ternary operator,因为if是一个表达式,如上面的简化示例所示。