我是 Swift 的新手,正在尝试学习初始化器的概念。我在下面创建了一个 SurveyQuestion 实例(即 cheeseQuestion)。
但是,创建之后,我想把'var text: String'改成“what kind of cheese do you like”?有人可以告诉我怎么做吗?
class SurveyQuestion{
var text: String
var response: String?
init(text: String){
self.text = text;
}
func ask(){
print(text)
}
}
let cheeseQuestion = SurveyQuestion(text: "do you like cheese?")
cheeseQuestion.ask()
cheeseQuestion.response = "yes, i do like cheese"
最佳答案
cheeseQuestion.text = "What kind of cheese do you like?"
没什么特别的。
关于swift - 初始化后如何更改实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35147171/