【问题标题】:How to change instance variable after initialisation?初始化后如何更改实例变量?
【发布时间】:2016-02-02 07:11:36
【问题描述】:

我是 Swift 新手,正在尝试学习初始化程序的概念。我在下面创建了一个 SurveyQuestion 实例(即 cheeseQuestion)。

但是,创建后,我想将 'var text: String' 更改为“您喜欢哪种奶酪”?有人能告诉我怎么做吗?

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"

【问题讨论】:

    标签: swift initialization


    【解决方案1】:
    cheeseQuestion.text = "What kind of cheese do you like?"
    

    没什么特别的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2011-03-18
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2010-10-01
      相关资源
      最近更新 更多