         }
         viewControllers.removeAll(keepCapacity: true)
         
-        viewControllerCount = self.datasource!.numberOfViewControllers()
-        for var i = 0; i < viewControllerCount; i++ {
-            let vc = self.datasource!.viewController(i)
+        if let cc = datasource?.numberOfViewControllers() {
+            self.viewControllerCount = cc
+            for var i = 0; i < viewControllerCount; i++ {
+                let vc = datasource!.viewController(i)
+                
+                addChildViewController(vc)
+                let size = contentScrollView.frame.size
+                vc.view.frame = CGRectMake(size.width * CGFloat(i), 0, size.width, size.height)
+                contentScrollView.addSubview(vc.view)
+                vc.didMoveToParentViewController(self)
+                viewControllers.append(vc)
+            }
+            generateTabs()
+            layout()
             
-            addChildViewController(vc)
-            let size = contentScrollView.frame.size
-            vc.view.frame = CGRectMake(size.width * CGFloat(i), 0, size.width, size.height)
-            contentScrollView.addSubview(vc.view)
-            vc.didMoveToParentViewController(self)
-            viewControllers.append(vc)
-        }
-        generateTabs()
-        layout()
-        
-        selectedIndex = min(viewControllerCount-1, selectedIndex)//Sanity check
-        if selectedIndex > 0 {// Happens for example in case of a restore
-            switchPage(selectedIndex, animated: false)
+            // Sanity check for restored selectedIndex values
+            selectedIndex = min(viewControllerCount-1, selectedIndex)
+            if selectedIndex > 0 {// Happens for example in case of a restore
+                switchPage(selectedIndex, animated: false)
+            }
         }
     }
     

        layoutTabIndicator()
    }
    
    /// Position the marker below the tab
    func layoutTabIndicator() {
        let labelF = tabLabels[selectedIndex].frame
        tabIndicator.frame = CGRectMake(labelF.origin.x, labelF.size.height-4, labelF.size.width, 4)
    }
    

        })
    }
    
    // MARK: Public methods
    public func reloadData() {
        for vc in viewControllers {
            vc.willMoveToParentViewController(nil)
