Objective c Swift数组到任意对象错误
尝试使用swift从函数返回数组时出现多个错误 如果我这样做:Objective c Swift数组到任意对象错误,objective-c,swift,typhoon,Objective C,Swift,Typhoon,尝试使用swift从函数返回数组时出现多个错误 如果我这样做: private dynamic func rootHomePageViewController() -> AnyObject? { return TyphoonDefinition.withClass(RootHomePageViewController.self) { (definition) in definition.useInitializer("style:navigation
private dynamic func rootHomePageViewController() -> AnyObject? {
return TyphoonDefinition.withClass(RootHomePageViewController.self) {
(definition) in
definition.useInitializer("style:navigationOrientation:options:") {
(initializer) in
initializer.injectParameterWith(UIPageViewControllerTransitionStyle.Scroll.rawValue) // must convert to raw value - watch for errors here
initializer.injectParameterWith(UIPageViewControllerNavigationOrientation.Vertical.rawValue) // must convert to raw value - watch for errors here
initializer.injectParameterWith(nil)
}
definition.injectMethod("setViewControllers:direction:animated:completion:", parameters: {
(method) in
method.injectParameterWith([self.pageViewController1(),self.pageViewController2(),self.pageViewController3()])
method.injectParameterWith(UIPageViewControllerNavigationDirection.Forward.rawValue)
method.injectParameterWith(false)
method.injectParameterWith(nil)
})
}
}
我收到错误:无法将表达式的类型“$T11”转换为类型“AnyObject?”
注意,self.pageViewController1()
是否返回任何对象?其他两个功能也是如此
如果将数组方法.injectParameter替换为(self.rootHomePageViewControllerPages)
,则使用此函数(创建数组):
我发现错误:无法将表达式的类型“Array”转换为类型“AnyObject”
无论我做什么,我都无法让函数正确返回(用AnyObject?
等重播[AnyObject]
)
基本上,我所要做的就是将数组注入UIPageViewController
的viewControllers
,但是在转换数组时存在一些问题
如果您有任何见解,我们将不胜感激。我使用以下方法解决了此阵列问题:
private dynamic func pageViewControllers() -> [AnyObject!] {
return [self.createViewController1(),self.createViewController2(),self.createViewController3()] as [AnyObject!]
}
其中每个createViewController()
函数返回AnyObject!
:
private dynamic func createViewController1() -> AnyObject! {
return TyphoonDefinition.withClass(TableViewController.self) {
(definition) in
definition.scope = TyphoonScope.Singleton
}
}
由于我们正在进行反射/元编程,它必须通过ObjC运行时。Swift数组通常透明地桥接到NSArray。但请尝试显式生成NSArray。例如NSArray。withObjects(a、b、c、d、nil)也就是self.pageViewController1()、self.pageController2()?这些方法是否返回typhondefinition?我想我已经包含了它们。它们确实返回typhondefinition(尽管函数实际上是AnyObject)。将立即尝试NSArray.withObjects。如果您回答您的问题,这可能会帮助其他用户。这也有助于我们提供良好的支持,因为我们可以看到哪些问题没有答案。完成。我实际上必须在返回各自的类型时删除它们,因为它返回了
台风定义
,无法将其强制转换到类。(见答覆)
private dynamic func createViewController1() -> AnyObject! {
return TyphoonDefinition.withClass(TableViewController.self) {
(definition) in
definition.scope = TyphoonScope.Singleton
}
}