How do I change the initial tab bar selection in swift?

In trying to find a solution I came across this answer:

[ http://stackoverflow.com/a/10441200/4856759 ]

It seems to accomplish what I want it to, but in objective C:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;

How do I do this in swift? My first attempt returns an error cannot convert the expression's type '$T4??' to type 'UITabBarController'

var tabBar: UITabBarController = self.window?.rootViewController
tabBar.selectedIndex = 1


Source: ios

Leave a Reply