Replacement for Objective-C pattern "lazy-getter-as-dependency-injection" in Swift?

With the exception of Typhoon , I was told that doing (or at least simulating) dependency injection in Objective-C was using the lazy getter model:

- (Foo)foo
{
    if (!_foo) {
        _foo = [Foo sharedInstance];
    }
    return _foo;
}

      

I've always hated this since you ended up with multiple instances of this 7-line piece of template code polluting each view controller. You cannot even hide it in a category as you cannot define properties in a category.

Until Swift gets annotations and introspection features and I can write something like:

@injected foo: Foo!

      

... what patterns are used by people in Swift to get a reference to shared model objects from multiple view controllers based on a storyboard?

+3


source to share





All Articles