Downcast from UITabBarItem? to UITabBarItem only unwraps optionals; did you mean to use '!'

匆匆过客 提交于 2019-12-08 12:56:32

问题


My code was working well in Xcode 6. But, after updating to Xcode 7 I got nearly 20 errors and 50 warnings.This might be some syntax change in Swift 2

Solved all those but can't figure out this one : Downcast from UITabBarItem? to UITabBarItem only unwraps optionals; did you mean to use '!'

let tabItems = tabBar.items as! [UITabBarItem]  // Error in  this line
    for (index, value) in enumerate(tabItems)
    {
        var imageName = imageNames[index]
        value.image = UIImage(named: imageName)
        value.imageInsets = UIEdgeInsetsMake(5.0, 0, -5.0, 0)
    }

Please help! Thanks in advance


回答1:


Try this:

if let tabItems = tabBar.items as [UITabBarItem]? {
    for (index, value) in tabItems.enumerate()
    {
        var imageName = imageNames[index]
        value.image = UIImage(named: imageName)
        value.imageInsets = UIEdgeInsetsMake(5.0, 0, -5.0, 0)
    }
}

You see the error because items now have type [UITabBarItem]?



来源:https://stackoverflow.com/questions/32847817/downcast-from-uitabbaritem-to-uitabbaritem-only-unwraps-optionals-did-you-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!