Swift - 打印出一个数字的当前值

时间:2016-02-08 06:24:52

标签: ios swift counter

我正在开发一个计数器应用程序,每次按下按钮都会计数。我已经完成了这个并且100%工作,我正在尝试做的是另一个按钮,当你按下它时,它显示控制台上的当前数字,但它只打印出var resolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); resolver.IgnoreSerializableAttribute = true; var config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = resolver; ,因为这是默认变量我将值赋给。

全班同学:

0

}

2 个答案:

答案 0 :(得分:0)

只需在numberUp操作中调用方法printButton:

var counterNumber = 0//this is a variable of class
@IBOutlet weak var counterLabel: UILabel!

@IBAction func printButton(sender: UIButton) {
    self.numberUp();
    print(self.counterNumber)
}

func numberUp(){
        self.counterNumber++;
        counterLabel.text = "\(self.counterNumber)"
    }

答案 1 :(得分:0)

  

使用' self' 关键字来调用实例变量。

class ViewController: UIViewController {

    // Properties
    var counterNumber:Int = 0    // Make this variable Global to Class

    // IBOutlets Properties
    @IBOutlet weak var counterLabel: UILabel!

    @IBAction func printButton(sender: UIButton) {
        self.numberUp()
        self.counterLabel.text = "\(self.counterNumber)"
        print("\n You Pressed ==> \(sender.title) button \(self.counterNumber) times")
    }

    func numberUp() {
        self.counterNumber += 1
        self.counterLabel.text = "\(counterNumber)"
    }
}