Search code examples
objective-cswifttyphoon

Swift Array to AnyObject Errors


I'm having multiple errors trying to return an array from a function with swift.

If I do this:

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

I get the error: Cannot convert the expression's type '$T11' to type 'AnyObject?'

Note, self.pageViewController1() returns AnyObject? as do each of the other 2 functions

If I replace the array method.injectParameterWith(self.rootHomePageViewControllerPages) there with this function (to create an array):

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

I get the error: Cannot convert the expression's type 'Array' to type 'AnyObject?'

No matter what I do, I cannot get the function to return correctly (Replaying [AnyObject] with AnyObject? , etc.

Basically all i'm trying to do is inject the array to viewControllers of UIPageViewController, but there is some issue with converting arrays.

Any insight would be appreciated!


Solution

  • I solved this array issue using:

    private dynamic func pageViewControllers() -> [AnyObject!] {
        return [self.createViewController1(),self.createViewController2(),self.createViewController3()] as [AnyObject!]
    }
    

    Where each createViewController() function returns AnyObject!:

    private dynamic func createViewController1() -> AnyObject! {
        return TyphoonDefinition.withClass(TableViewController.self) {
            (definition) in
            definition.scope = TyphoonScope.Singleton
        }
    }