【问题标题】:Unable to access index of tabBar item using swift无法使用 swift 访问 tabBar 项的索引
【发布时间】:2014-09-16 12:23:02
【问题描述】:

环境:xcode 6GM,语言 Swift。 我在 xcode 6 beta2 中使用此代码设置 tabBar 项的图像颜色

var cameraTab : UITabBarItem = self.tabBar.items[1] as UITabBarItem

但现在在 xcode 6GM 中出现错误。 错误:[AnyObject]?没有名为“下标”的成员

【问题讨论】:

  • tabBaritems 都是可选的。

标签: ios swift xcode6 uitabbaritem


【解决方案1】:

items 是可选的 - 你可以这样做:

   if let items = self.tabBar.items {
    println("\(items[1])")
  }

  var cameraTab : UITabBarItem = self.tabBar.items![1] as UITabBarItem

【讨论】:

  • 谢谢!非常简单的答案!
【解决方案2】:

items 属性对于tabBar 是可选的。尝试可选链接:

var cameraTab : UITabBarItem = self.tabBar.items?[1] as UITabBarItem

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    相关资源
    最近更新 更多