winston
winston

Reputation: 3100

Expected Sequence expression swift 3

I'm using a loop to set the original image for tab bar items so the unselected state isn't gray (my original icons are white). However, it looks like the recent Xcode 8 update broke the code:

for (items in 0  ..< tabBar.items!.count ){
    let tabItemIndex = tabBar.items![items]
    tabItemIndex.image = tabItemIndex.image!.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}

}

I'm getting the following errors on the first line: Expected 'in' after for-each pattern, Expected Sequence expression for for-each loop, and Expected pattern.

Can anyone please help me fix this solution? It worked great until today.

Thanks!!

Upvotes: 1

Views: 3576

Answers (3)

andyvn22
andyvn22

Reputation: 14824

In Swift, unlike in Objective-C, there are no parentheses involved in this structure. Remove the parentheses to fix your syntax error.

However, there are style and safety issues here other than the syntax error: see picciano's answer for a much cleaner and safer way to rewrite the loop.

Upvotes: 0

matt
matt

Reputation: 535306

for x in y is an actual expression in Swift. You cannot break it up with parentheses, for (x in y) — that separates the for from the in and causes the expression to seem like nonsense to the compiler.

So, delete the parentheses and all will be well.

Upvotes: 9

picciano
picciano

Reputation: 22701

You have some issues on how you're creating your loop, and some very unsafe forced unwrapping. Try this:

if let items = tabBar.items {
    for tabBarItem in items {
        if let image = tabBarItem.image {
            tabBarItem.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        }
    }
}

Or even cleaner, like this:

tabBar.items?.forEach { tabBarItem in
    if let image = tabBarItem.image {
        tabBarItem.image = image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    }
}

Upvotes: 2

Related Questions