inheriting var in other class ?

My brain is fried from heat, not even sure how to pose the question.


I have a class that has a var let's say "var isRunning: Bool = false", within it's parent another class instance with the same var where I want it to be controlled from. If I link the two, initially it's reading the var but changing later has no affect.


Sorry, I know this is probably a basic question.

Accepted Reply

Could be something like this:


protocol FromCtoB {
    func updateB(val: Bool)
}

class A {
    let classB = B()
    let classC = C()
   
    init() {
        classC.delegate = classB
        // class1.isRunning = class2.isRunning
        classC.change()
        print("classB.isRunning", classB.isRunning, "classC.isRunning", classC.isRunning)
    }
}

class B : FromCtoB {
    var isRunning: Bool = false

    // Protocol implementation
    func updateB(val: Bool) {
        isRunning = val
    }
}

class C {
    var isRunning: Bool = false
    var delegate: FromCtoB?
   
    func change() {
        delegate?.updateB(val: isRunning)
    }
}


and

let classA = A()

yields

classB.isRunning false classC.isRunning false



if you change line 27

var isRunning: Bool = true

let classA = A()

yields

classB.isRunning true classC.isRunning true



Is it what you are looking for ?

Replies

Not sure I understand.


I have a class that has a var let's say "var isRunning: Bool = false",

So, you have


class A {
     var isRunning: Bool = false
}


within it's parent another class instance

is class A a subclass of a parent B ? so it would be

class A : B {
     var isRunning: Bool = false
}


Now, this is not clear at all:

class instance with the same var where I want it to be controlled from


Do you mean

class B {
     var anotherBool: Bool = false
}

and you want a change to anotherBool to change isRunning ? I'm lost.


Or do you want

class B {
     var isRunning: Bool = false
}


Then, no need to redefine in subclass A…


Please clarify so that we can continue the discussion.


May be you should use delegation or notification (or a more direct solution) here, but we need first to clarify your intent.

Sorry not inheriting the class. So parent has 2 different classes both with same variable but one has the controlling to affect the other.


class A {
     let class1 = B()
     let class2 = C()
     init() {
          class1.isRunning = class2.isRunning
     }
}

class B {
     var isRunning: Bool = false
}
class C {
     var isRunning: Bool = false
     func change() { ... }
}

And what do you want : set the value of class2.isRunning to class1.isRunning when you call change ?


If so, I would use delegation here.

Could be something like this:


protocol FromCtoB {
    func updateB(val: Bool)
}

class A {
    let classB = B()
    let classC = C()
   
    init() {
        classC.delegate = classB
        // class1.isRunning = class2.isRunning
        classC.change()
        print("classB.isRunning", classB.isRunning, "classC.isRunning", classC.isRunning)
    }
}

class B : FromCtoB {
    var isRunning: Bool = false

    // Protocol implementation
    func updateB(val: Bool) {
        isRunning = val
    }
}

class C {
    var isRunning: Bool = false
    var delegate: FromCtoB?
   
    func change() {
        delegate?.updateB(val: isRunning)
    }
}


and

let classA = A()

yields

classB.isRunning false classC.isRunning false



if you change line 27

var isRunning: Bool = true

let classA = A()

yields

classB.isRunning true classC.isRunning true



Is it what you are looking for ?

Ok thanks.

Note that if you don't need to implement change() to update B with C value, your code should work as well


class AA {
    let class1 = BB()
    let class2 = CC()
    init() {
        class1.isRunning = class2.isRunning
        print("class1.isRunning", class1.isRunning, "class2.isRunning", class2.isRunning)
    }
}


class BB {
    var isRunning: Bool = false
}
class CC {
    var isRunning: Bool = false
//    func change() { ... }
}


let classAA = AA()

I think it's because it's on a different thread. I'll try to explain ...


The main class has an instance of an instantiated WindowController which has the variable which will change, and WC's can only be created in the main thread. The second instance is within a "DispatchQueue.global(qos: .utility).async {...}" which is where I linked the variables.


If I don't use a DispatchQueue I can't access the button which will change the var. I just see the waiting cursor icon.

Is the delegation working as needed ?

Yes the delegate works fine thank u.


There is another problem but will make another post.