如何在函数外部使用变量

时间:2017-07-01 01:50:28

标签: swift function variables

override func viewDidLoad() {
    super.viewDidLoad()

    SuccessMark.maximumValue = 100
    SuccessMark.value = successMark
}

override func viewDidDisappear(_ animated: Bool) {

    let successMark = SuccessMark.value
}

1 个答案:

答案 0 :(得分:0)

在任何函数之外的viewcontroller类中声明变量。这样整个viewcontroller类就能够读取和写入变量。

Class ViewController: UIViewController {

var successMark: YourCustomType! // either initialise it empty or use optionals as needed, YourCustomType! or YourCustomType?
    override func viewDidLoad() {
        super.viewDidLoad()

    SuccessMark.maximumValue = 100
    SuccessMark.value = successMark
}

override func viewDidDisappear(_ animated: Bool) {

        successMark = SuccessMark.value
   }
}