Search code examples
iosobjective-cdependency-injectiontyphoon

TyphoonOptionMatcher with conformsToProtocol


It would be nice if I could use matcher to select definition based on protocol, for example:

- (UIViewController *)viewControllerForViewModel:(id <IViewModel>)viewModel
{
    return [TyphoonDefinition withOption:viewModel matcher:^(TyphoonOptionMatcher *matcher)
    {
        [matcher conformsToProtocol:@protocol(ILoginViewModel) use:[self loginViewController]];
        [matcher conformsToProtocol:@protocol(IMainViewModel) use:[self mainScreenViewController]];
        [matcher defaultUse:[self emptyScreen]];
    }];
}

Is it possible to do with what Typhoon has right now or maybe with some workarounds?

Thank you.

Edit: I've made some additions to TyphoonOptionMatch and TyphoonOptionMatcher to have conformsToProtocol option, is it worth it to create a PR?

TyphoonOptionMatch

@property (nonatomic) Protocol *proto;
....
+ (id)matchWithConformsToProto:(Protocol *)proto injection:(id)injection;

TyphoonOptionMatcher

- (void)conformsToProto:(Protocol *)aProtocol use:(id)injection
...
- (TyphoonOptionMatch *)matchForValue:(id)value {
...
    BOOL isConformProto = (match.proto && [value conformsToProtocol:match.proto]);

Solution

  • This feature is out in 3.4.5 release

    [matcher caseConformsToProtocol:@protocol(IMainViewModel) 
                                use:[self mainScreenViewController]];