1

我在尝试使用 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?等)

基本上我要做的就是将数组注入到viewControllersof UIPageViewController,但是转换数组存在一些问题。

任何见解将不胜感激!

4

1 回答 1

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
    }
}
于 2015-01-22T04:15:47.480 回答