假设我有两个班级Foo
和Bar
. Bar
我可以独立于构建 a Foo
,但Foo
需要 a Bar
。在 Typhoon 中,我定义了这两个组件并在我的应用程序中使用它们,但是在运行时构建我传入的 a 似乎作为 a而不是我传入的Foo
那个传递给另一个组件。barId
Bar
<TyphoonInjectionByRuntimeArgument: 0x15ea4670, type=Undifined>
NSString
我可能做错了。
我应该怎么做?
@implementation Assembly
-(Foo *)fooWithFooId:(NSString *)fooId andBarId:(NSString *)barId {
return [TyphoonDefinition withClass:[Foo class] configuration:^(TyphoonDefinition* definition) {
[definition useInitializer:@selector(initWithFooId:andBar:) parameters:^(TyphoonMethod *initializer) {
[initializer injectParameterWith:fooId];
[initializer injectParameterWith:[self barWithId:barId]];
}];
}];
}
-(Bar *)barWithBarId:(NSString *)barId {
return [TyphoonDefinition withClass:[Bar class] configuration:^(TyphoonDefinition* definition) {
[definition useInitializer:@selector(initWithBarId:) parameters:^(TyphoonMethod *initializer) {
[initializer injectParameterWith:barId];
}];
}];
}
@end
////////
@implementation Foo
-(instancetype) initWithFooId:(NSString *)fooId andBar:(Bar *)bar
{
self = [super init];
self.fooId = fooId;
self.bar = bar;
return self;
}
@end
@implementation Bar
-(instancetype) initWithBarId:(NSString *)barId
{
self = [super init];
self.barId = barId;
return self;
}
@end
更新:
进一步审查后,该问题似乎是由在定义中使用运行时参数为另一个定义构建另一个参数引起的。看下面的用法NSString
:
@implementation Assembly
-(Foo *)fooWithFooId:(NSString *)fooId andBazId:(NSString *)bazId {
return [TyphoonDefinition withClass:[Foo class] configuration:^(TyphoonDefinition* definition) {
[definition useInitializer:@selector(initWithFooId:andBaz:) parameters:^(TyphoonMethod *initializer) {
[initializer injectParameterWith:fooId];
[initializer injectParameterWith:[self bazWithPathToBaz:[NSString stringWithFormat:@"/some/path/to/baz/%@", bazId]]];
}];
}];
}
-(Baz *)bazWithPathToBaz:(NSString *)bazPath {
return [TyphoonDefinition withClass:[Baz class] configuration:^(TyphoonDefinition* definition) {
[definition useInitializer:@selector(initWithBazPath:) parameters:^(TyphoonMethod *initializer) {
[initializer injectParameterWith:bazPath];
}];
}];
}
@end
当被调用时,传入初始化器的路径将是"/some/path/to/baz/<TyphoonInjectionByRuntimeArgument: 0x15ea4670, type=Undifined>"