appsquickly / typhoon

Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
https://pilgrim.ph
Apache License 2.0
2.7k stars 270 forks source link

TyphoonDefinition.withClass + classOrProtocolForAutoInjection => Making both work together #590

Closed AdithyaReddy closed 6 years ago

AdithyaReddy commented 6 years ago

Could you please tell me how would I resolve a component-by-type using TyphoonDefinition? Or am I missing something in the TyphoonFactoryDefinition?

func getClass<T>(props: NSMutableDictionary, type: T.Type) -> Any {

    Trial: 1-> When I try to init a class with TyphoonFactoryDefinition, am unable to resolve the class by type/key

    /*return TyphoonFactoryDefinition.withClass(SearchCategoryViewController.self, configuration: { (definition) in
        props.allKeys.forEach { (key) in
            if let key = key as? String, let value = props[key] {
                definition?.injectProperty(NSSelectorFromString(key), with: value)
            }
        }
        definition?.autoInjectionVisibility = TyphoonAutoInjectVisibility.byClass
        if let definition = definition as? TyphoonFactoryDefinition {
            definition.classOrProtocolForAutoInjection = SearchCategoryViewController.self
        }
    })*/

    Trial: 2 -> Here the definition gets registered and am able to resolve component by key,    BUT not by type because the property 'classOrProtocolForAutoInjection' is not available under TyphoonDefinition

    return TyphoonDefinition.withClass(SearchCategoryViewController.self, configuration: { (definition) in
        props.allKeys.forEach { (key) in
            if let key = key as? String, let value = props[key] {
                definition?.injectProperty(NSSelectorFromString(key), with: value)
            }
        }
        definition?.autoInjectionVisibility = TyphoonAutoInjectVisibility.byClass
        if let definition = definition as? TyphoonFactoryDefinition {
            /* control doesn't reach here cause it is not TyphoonFactoryDefinition */
            definition.classOrProtocolForAutoInjection = SearchCategoryViewController.self
        }
    })
}

And is there a way to access 'classOrProtocolForAutoInjection' in TyphoonDefinition?

Thank you :)

alexgarbarev commented 6 years ago

Hey @AdithyaReddy, this is not intended use of TyphoonDefinition, there is no way to use classOrProtocolForAutoInjection with TyphoonDefinition. Your code looks very custom, so I recommend you learn Typhoon sources and create own TyphoonDefinition subclass with isCandidateForInjectedProtocol and isCandidateForInjectedClass:includeSubclasses: methods overridden. See @interface TyphoonDefinition (/* Infrastructure */) category