简体   繁体   中英

Swift 3 - didSet for the opposite direction?

Let's say I have these two classes:

class A {}
class B{
   var myA:A?
}

I want to know when that happens:

let b = B()
let a = A()

b.myA = a    // <<<---- I want to observe this from my A class

I would also like to know who is the owner(In this case, b ).

Sort of like a didSetSelfAsAProperty(ofOwner:AnyObject).

What I am trying to achieve: We have MVVM architecture in our project. I noticed that I need to set both directions when initializing an instance of a view model/view.I need to tell the view model who's its view and vice versa.

Any ideas?

It seems that you're looking for the Observer Pattern. You could achieve it with either callbacks, KVO or third party libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM