1

我在注入 BOOL 属性时遇到问题。

我尝试了下一个:

- (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]];
            }];
}

但它在运行时与EXC_BAD_ACCESS. 实际上,它与 BOOL 属性无关,而是带有值本身的注入属性。看起来我对Typhoon使用的假设是错误的。

4

2 回答 2

4

运行时参数必须始终是一个对象——而不是原始类型!

正确的组装在这里:

- (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];
            }];
}

其中 firstController 属性可以有 BOOL,但是在调用 assmebly 接口时,您必须使用 NSNumber 包装器:

[assembly emotionControllerWithItem:item firstController:@YES];
于 2014-07-03T10:53:27.340 回答
1

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.

于 2014-07-03T10:24:44.667 回答