Swift数组到AnyObject错误

时间:2015-01-22 01:18:55

标签: objective-c swift typhoon

我尝试使用swift从函数返回数组时遇到多个错误。

如果我这样做:

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)
        })
    }
}

我收到错误:Cannot convert the expression's type '$T11' to type 'AnyObject?'

注意,self.pageViewController1()返回AnyObject?和其他两个函数一样

如果我用这个函数替换数组method.injectParameterWith(self.rootHomePageViewControllerPages)(创建一个数组):

private dynamic func rootHomePageViewControllerPages() -> [AnyObject] {
    return [self.pageViewController1(),self.pageViewController2(),self.pageViewController3()] as [AnyObject]!
}

我收到错误:Cannot convert the expression's type 'Array' to type 'AnyObject?'

无论我做什么,我都无法正常返回功能(使用[AnyObject]重播AnyObject?等。

基本上我所要做的就是将数组注入viewControllers的{​​{1}},但转换数组存在一些问题。

任何见解都将受到赞赏!

1 个答案:

答案 0 :(得分:1)

我使用以下方法解决了这个数组问题:

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
    }
}