简体   繁体   中英

Typhoon inject property

I have problems with injecting BOOL property.

I've tried next:

- (id)emotionControllerWithItem:(MDDiaryItem *)item firstController:(BOOL)isFirst
{
    return [TyphoonDefinition withClass:[MDEmotionViewController class]
                          configuration: ^(TyphoonDefinition *definition) {
                [definition useInitializer:@selector(initWithAnalytics:) parameters: ^(TyphoonMethod *initializer) {
                    [initializer injectParameterWith:[_services analytics]];
                }];
                [definition injectProperty:@selector(setItem:) with:item];
                [definition injectProperty:@selector(setFirstController:) with:[NSNumber numberWithBool:isFirst]];
            }];
}

But it crashes in runtime with EXC_BAD_ACCESS . Actually it is nothing about BOOL property but injection properties with values itself. It looks like my assumption about Typhoon usage is wrong.

Runtime arguments must be always an object - not primitive type!

The correct assembly is here:

- (id)emotionControllerWithItem:(MDDiaryItem *)item firstController:(NSNumber *)isFirst
{
    return [TyphoonDefinition withClass:[MDEmotionViewController class]
                          configuration: ^(TyphoonDefinition *definition) {
                [definition useInitializer:@selector(initWithAnalytics:) parameters: ^(TyphoonMethod *initializer) {
                    [initializer injectParameterWith:[_services analytics]];
                }];
                [definition injectProperty:@selector(setItem:) with:item];
                [definition injectProperty:@selector(setFirstController:) with:isFirst];
            }];
}

where firstController property can has BOOL, but when calling assmebly interface, you have to use NSNumber wrapper:

[assembly emotionControllerWithItem:item firstController:@YES];

Generally you can inject as follows:

[definition injectProperty:@selector(isFirst) with:[NSNumber numberWithBool:YES]];

But probably nicer:

[definition injectProperty:@selector(isFirst) with:@(YES)];

However, runtime arguments have the following limitations:

  • You can't call any methods on the runtime argument.
  • Run-time arguments can't be primitives.

You could either work-around this, or drop back to writing your own factory class to be used in place of the assembly interface.

The answer by @Aleksey shows a workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM