无法下标类型'[OnboardingViewController]'的值

时间:2015-12-02 20:01:30

标签: ios swift compiler-errors

我正在创建一个ViewControllers数组,而XCode让我试图通过括号表示法来访问它们。以下代码实例化两个OnboardingViewControllers(UIViewController的子类)。

var controllers = [OnboardingViewController]()
var controller = OnboardingViewController()
let totalOnboardingPages = 2

override func viewDidLoad() {
    super.viewDidLoad()
    populateControllersArray()
    createPageViewController()
}

func populateControllersArray(){
    for i in 0...totalOnboardingPages-1 {
        controllers.append(getPageController(i))
    }
}

func getPageController(itemIndex: Int) -> OnboardingViewController {
    var controller = OnboardingViewController()

    switch itemIndex {
    case 0:
            // Welcome
            controller = storyboard!.instantiateViewControllerWithIdentifier("OnboardingWelcomeViewController") as! OnboardingViewController
    case 1:
        // Find Facebook Friends
        controller = storyboard!.instantiateViewControllerWithIdentifier("OnboardingFacebookViewController") as! OnboardingViewController

    default: ()
    }

    controller.itemIndex = itemIndex
    return controller
}

func createPageViewController() {
    let pageController = storyboard!.instantiateViewControllerWithIdentifier("OnboardingPageViewController") as! UIPageViewController
    pageController.dataSource = self

    // BREAKS ON THIS LINE
    pageController.setViewControllers(controllers[0], direction: .Forward, animated: false, completion: nil)

    pageViewController = pageController
    addChildViewController(pageViewController!)
    view.addSubview(pageViewController!.view)
    pageViewController!.didMoveToParentViewController(self)

}

错误发生在createPageViewController()

pageController.setViewControllers(controllers[0], direction: .Forward, animated: false, completion: nil)
  

无法下标'[OnboardingViewController]'

类型的值

好的,但是当我在该行设置断点时,我可以po controllers[0]并获取我的控制器:

<MyApp.OnboardingWelcomeViewController: 0x618000086080>

事实上,我所拥有的下一个功能是XCode不会抱怨的。

return controllers[controller.itemIndex - 1]

controllers不是可选的,我不知道这里发生了什么。我已经清理,删除了DerivedData目录并重新启动了XCode。救命啊!

1 个答案:

答案 0 :(得分:0)

解决方案是将ViewControllers数组传递给$code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 。我更正的代码是:

setViewControllers()

感谢Paulw11的有用评论。