Skip to content

How to register a instance directly? #255

Answered by hmlongco
faimin asked this question in Q&A
Discussion options

You must be logged in to vote

The factory must exist. If you have to do this, I'd consider...

internal struct WeakObject<T: AnyObject> {
    weak var object: T?
}

extension Container {
    var referenceMyAVC: Factory<WeakObject<MyAVC>> {
        self { .init(object: nil) }
    }
}

class MyAVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        Container.shared.referenceMyAVC.register { .init(object: self) }
    }
}

class MyDVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        weak var myAVC = Container.shared.referenceMyAVC.resolve().object
    }
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by faimin
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants