Lukas Köhl
Lukas Köhl

Reputation: 1579

Swift: global variable

Hey Guys I have to different functions and I want to access the variables in them. Heres my code:

func calculate(){
    var solution=a+b+c
}


 func farbeWechsel1() {
    if(zug==0){
        button1.setBackgroundImage(rot, forState: .Normal)
        zug++
        firstRow.removeAtIndex(0)
        firstRow.insert(zahlRot, atIndex: 0)
        var a:Int? = zahlRot.toInt()
        calculate()
        if(solution==3){

        }
  }

I want to get access to the solution variable from the farbeWechsel function. Hopefully you guys can help me. I am not so experienced in Swift and want to get started.

Upvotes: 1

Views: 435

Answers (1)

zaph
zaph

Reputation: 112857

Create a class that both func belong to and make the variable an instance variable (property).

Example (there are several problems in the OP's code so the example is slightly different):

class A {
    var solution = 0

func calculate() {
    var a = 1
    var b = 2
    var c = 3

    self.solution = a+b+c
}


func farbeWechsel1() {
    if(self.solution == 3) {

    }
}

}

Upvotes: 2

Related Questions