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

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

[ ]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.