gpt4 book ai didi

ios - 台风 + swift : Crash when using run-time arguments

转载 作者:行者123 更新时间:2023-11-28 06:58:48 27 4
gpt4 key购买 nike

我目前正在尝试使用 Typhoon 将依赖注入(inject)添加到我的应用程序中,该应用程序现在一半是 Swift,一半是遗留的 Objective-C。我正在尝试从我的程序集中的 Storyboard实例化 View Controller ,但在下面的崩溃中运行。我在这里阅读https://github.com/appsquickly/Typhoon/issues/293使用 NSString 可以解决问题,但正如您在下面看到的那样,我已经这样做了,但并没有太大帮助……有什么想法吗?

dynamic func typhoonStoryboard(name: NSString) -> AnyObject {
return TyphoonDefinition.withClass(TyphoonStoryboard.self, configuration: { (definition) -> Void in
definition.useInitializer("storyboardWithName:factory:bundle:", parameters: { (initializer) -> Void in
initializer.injectParameterWith(name)
initializer.injectParameterWith(self)
initializer.injectParameterWith(NSBundle.mainBundle())
})
})
}

dynamic func viewController(withStoryboardName storyboardName: NSString, storyboardIdentifier: NSString) -> AnyObject {
return TyphoonDefinition.withFactory(self.typhoonStoryboard(NSString(string: storyboardName)), selector: "instantiateViewControllerWithIdentifier:", parameters: { (method) -> Void in
method.injectParameterWith(NSString(string: storyboardIdentifier))
}, configuration: { (definition) -> Void in

})
}

知道哪里出了问题吗?如您所见,我已经在使用 NSString 进行变通,但它仍然因以下异常异常而崩溃...

2015-09-15 09:26:32.484 MyApp[64855:5393006] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You can't call a method on the runtime argument being passed in. It has to be passed in as-is'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ae0ff65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a42edeb objc_exception_throw + 48
2 CoreFoundation 0x000000010ae0fe9d +[NSException raise:format:] + 205
3 MyApp 0x00000001052282b3 -[TyphoonInjectionByRuntimeArgument forwardingTargetForSelector:] + 67
4 CoreFoundation 0x000000010ad65c4e ___forwarding___ + 158
5 CoreFoundation 0x000000010ad65b28 _CF_forwarding_prep_0 + 120
6 libswiftCore.dylib 0x000000010bff54b3 _TTSf4g_d___TFSSCfMSSFT12_cocoaStringPSs9AnyObject__SS + 131
7 libswiftCore.dylib 0x000000010bfbce33 _TFSSCfMSSFT12_cocoaStringPSs9AnyObject__SS + 19
8 libswiftFoundation.dylib 0x000000010c3e0af9 _TToFE10FoundationCSo8NSStringcfMS0_FT6stringS0__S0_ + 41
9 libswiftFoundation.dylib 0x000000010c3e0a3a _TFE10FoundationCSo8NSStringCfMS0_FT6stringS0__S0_ + 58
10 MyApp 0x0000000104cc890d _TFC8MyApp22ViewControllerAssembly14viewControllerfS0_FT18withStoryboardNameCSo8NSString20storyboardIdentifierS1__PSs9AnyObject_ + 157
11 MyApp 0x0000000104cc90cf _TToFC8MyApp22ViewControllerAssembly14viewControllerfS0_FT18withStoryboardNameCSo8NSString20storyboardIdentifierS1__PSs9AnyObject_ + 79
12 CoreFoundation 0x000000010acfe85c __invoking___ + 140
13 CoreFoundation 0x000000010acfe6ae -[NSInvocation invoke] + 286
14 CoreFoundation 0x000000010ad8f016 -[NSInvocation invokeWithTarget:] + 54
15 MyApp 0x000000010520ea76 objc_msgSend_InjectionArguments + 454
16 MyApp 0x000000010520e83e -[TyphoonAssemblyDefinitionBuilder definitionForKey:] + 254
17 MyApp 0x000000010520e61f -[TyphoonAssemblyDefinitionBuilder populateCacheWithDefinitionForKey:] + 111
18 MyApp 0x000000010520e01b -[TyphoonAssemblyDefinitionBuilder builtDefinitionForKey:args:] + 203
19 MyApp 0x000000010520df20 -[TyphoonAssemblyDefinitionBuilder buildDefinitionForKey:] + 64
20 MyApp 0x000000010520de43 __49-[TyphoonAssemblyDefinitionBuilder populateCache]_block_invoke + 163
21 CoreFoundation 0x000000010ad6f0df __51-[__NSSetM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 79
22 CoreFoundation 0x000000010ad6efea -[__NSSetM enumerateObjectsWithOptions:usingBlock:] + 202
23 MyApp 0x000000010520dd69 -[TyphoonAssemblyDefinitionBuilder populateCache] + 185
24 MyApp 0x000000010520dc1a -[TyphoonAssemblyDefinitionBuilder builtDefinitions] + 74
25 MyApp 0x000000010520a976 -[TyphoonAssembly definitions] + 54
26 MyApp 0x000000010520ff1a -[TyphoonBlockComponentFactory registerAllDefinitions:] + 90
27 MyApp 0x000000010520fd78 -[TyphoonBlockComponentFactory buildAssembly:] + 104
28 MyApp 0x000000010520fc10 -[TyphoonBlockComponentFactory initWithAssemblies:] + 544
29 MyApp 0x000000010520f8b7 +[TyphoonBlockComponentFactory factoryWithAssemblies:] + 87
30 MyApp 0x00000001052340d2 +[TyphoonStartup requireInitialFactory] + 194
31 MyApp 0x00000001052345ad __60+[TyphoonStartup swizzleSetDelegateMethodOnApplicationClass]_block_invoke + 93
32 UIKit 0x0000000108a8825b -[UIApplication _setDelegate:assumeOwnership:] + 51
33 UIKit 0x0000000108a93cea _UIApplicationMainPreparations + 1618
34 UIKit 0x0000000108a93647 UIApplicationMain + 124
35 MyApp 0x0000000104b1f80f main + 111
36 libdyld.dylib 0x000000010c5a492d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

Typhoon 是一个相当典型的内省(introspection)和反射类型的 DI 容器,针对 Objective-C 进行了优化。 (也适用于 Swift)。

  • 启动时:Typhoon 从所有程序集中收集有关如何构建对象的信息
  • 在运行时:组装接口(interface)位于 TyphoonComponentFactory 前面,它知道如何根据所有这些收集到的规则发出组件。

获取组件:

Rules parsed on startup->post processing->factory emits built objects

因此:在运行时参数的情况下,我们不能对它们调用任何方法,或者在传入后以其他方式操作它们,因为这与 Typhoon 构建对象实例化蓝图时的启动阶段无关。

如果您声明 NSString 类型的运行时参数,并直接传递它而不进行操作(而不是 NSString(string: storyboardIdentifier)),它应该可以正常工作。

关于ios - 台风 + swift : Crash when using run-time arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32580206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com