1

I want to iterate through view controllers of navigation controller in swift. For that I wrote a for loop like this

for navController in tabBarController?.viewControllers {
     //some process
}

tabBarController is a UITabBarController. But I am getting error like '$T4??' does not have a member named 'Generator' Whats wrong with the code?

0

1 Answer 1

6

Optional chaining results in optional array of view controllers. Optional arrays do not conform to those protocols for be iterated with for..in loop. Try:

if let viewControllers = tabBarController?.viewControllers {
    for viewController in viewControllers {
        // some process
    } 
}
Sign up to request clarification or add additional context in comments.

3 Comments

for viewController in tabBarController?.viewControllers as [UIViewController] {} also works.
@Swipesight it shows error viewController '[UIViewController]?' does not have a member named 'Generator'
@Swipesight sorry my bad my object was tabController not tabBarController. tabBarController taking view controllers property I think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.