swift Class variables not being changed when accessed from another class

266 views Asked by At

I've got a function which updates the rgb variables. When its called within the class it works fine but if I try to access it through another class wont do anything.

var red : CGFloat! = 0.5;
var green : CGFloat! = 0.5;
var blue : CGFloat! = 0,5;

func updateColour(r:CGFloat, g:CGFloat, b:CGFloat){
    red = r;
    green = g;
    blue = b;
}

I'm trying to set the values from another class using the code below. I used a print command to make sure the function is being called but the variables won't update.

    let canvas = canvasViewController() as canvasViewController;
    canvas.updateColour(r: red, g: green, b: blue)

Not sure if I'm setting the variables incorrectly or accessing the class wrong but any help will be greatly appreciated.

1

There are 1 answers

0
Mohammed Elrashidy On BEST ANSWER

Actually, I think you did something wrong, you need to revisit your code, or share more code with us.

BTW, I tried your code in Swift 3.0 in Playground and everything works fine, try it yourself:

class canvasViewController {
    var red : Float! = 0.5;
    var green : Float! = 0.5;
    var blue : Float! = 0.5;

    func updateColour(r:Float, g:Float, b:Float){
        red = r;
        green = g;
        blue = b;
    }
}
class anotherClass {
    func checkUpdateColour() {
        let canvas = canvasViewController()
        canvas.updateColour(r: 0.25, g: 0.25, b: 0.25)
        canvas.red
    }
}
var another = anotherClass()
another.checkUpdateColour()