本文介绍了从UITabBarItem垂头丧气?到UITabBarItem仅解包可选;你的意思是使用'!'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在Xcode 6中运行良好.但是,在更新到Xcode 7之后,我收到了将近20个错误和50条警告.这可能是Swift 2中的某些语法更改

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

解决了所有这些问题,但无法解决:从UITabBarItem下调?到UITabBarItem仅解包可选;您是不是要使用'!'

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)
    }

请帮助!预先感谢

推荐答案

尝试一下:

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)
    }
}

您看到此错误,因为items现在具有类型[UITabBarItem]?

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

这篇关于从UITabBarItem垂头丧气?到UITabBarItem仅解包可选;你的意思是使用'!'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:31