Access global var

Asked

Viewed 31 times

0

I have the following problem: I have the following code

var myGlobal : String
func nomeDaFuncao () {
    // Preciso acessar a global
    myGlobal = "fff"
}

How can I access the global within a function or an action ? Thank you

1 answer

2


You can use the self if using the attribute in the same class

self.myGlobal = "VALOR"

Example:

class ViewController: UIViewController {
    var myGlobal : String?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.myGlobal = "Teste"
    }
}

Remember to use ? for non-mandatory attributes and ! for mandatory taxes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.