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 269 forks source link

Injected property is nil #539

Open pinchukvd opened 7 years ago

pinchukvd commented 7 years ago

Hi I'm getting a nil property after injection. Here is my code:

@implementation MainVCAssembly

-(MainVCViewModel*)viewModel
{
    return [TyphoonDefinition withClass:[MainVCViewModel class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(bankDataService) with:[self bankDataService]];
    }];
}

-(id<UserDataService>)bankDataService
{
    return [self.servicesAssembly userDataService];
}

UserDataServiceImplementation is injected successfuly. But when injecting an operationsAssembly in userDataService

@implementation ServicesAssemblyImplementation

-(id<UserDataService>)userDataService
{
    return [TyphoonDefinition withClass:[UserDataServiceImplementation class] configuration:^(TyphoonDefinition *definition) {
           [definition injectProperty:@selector(operationsAssembly) with:[self operationsAssembly]];
    }];
}

-(id<OperationsAssembly>)operationsAssembly
{
    return [TyphoonDefinition withClass:[OperationsAssemblyImplementation class] configuration:^(TyphoonDefinition *definition) {
    }];
}

i'm getting a nil in setter

@implementation UserDataServiceImplementation

-(void)setOperationsAssembly:(id<OperationsAssembly>)operationsAssembly
{
    NSLog(@"set operationsAssembly in UserDataServiceImplementation");
    if(!operationsAssembly)
    {
        NSLog(@"operationsAssembly is nil");
    }
}
alexgarbarev commented 7 years ago

Your code looks correct from first sight. Try something of this: 1) Try to not override operationsAssembly setter. 2) Make sure that operationsAssembly property exists (not only setter/getter) 3) try to inject setOperationsAssembly via methodInjection instead 4) try latest version with

  pod 'Typhoon', :git => 'https://github.com/appsquickly/Typhoon.git', :branch => 'v4'
pinchukvd commented 7 years ago

Tried first three and still getting nil. With v4 got problems with manual activation. Getting TyphoonAssemblyAccessor instead of MainVCAssembly instance

MainVCAssembly *assembly=(MainVCAssembly*)[[self.controllersAssembly activated] mainViewControllerAssembly];

threfore it's unrecognized selector sent to instance

alexgarbarev commented 7 years ago

Getting TyphoonAssemblyAccessor instead of MainVCAssembly instance

That's ok

threfore it's unrecognized selector sent to instance

Because you should pass collaborating assemblies inside activation. See activateWithCollaboratingAssemblies for reference

alexgarbarev commented 7 years ago

Ah, you are right regarding "threfore it's unrecognized selector sent to instance". Latest pull request broke that branch.