从Swift中的IBOutletCollection获取一个项目

Sha*_* TK 1 uibutton ios iboutletcollection swift

我收到一个错误,因为"没有更多上下文,表达的类型是模糊的"

这是我的代码

@IBOutlet var customerDashboardButtons:[NSArray]?

var predicate = NSPredicate(format: "SELF.tag == %d", tag)
var filteredButtons = customerDashboardButtons!.filter { predicate.evaluateWithObject($0) };
if 0 < filteredButtons.count {
      var button = customerDashboardButtons!.first
      button.hidden = true // getting an error in this line as "Type of expression is ambiguous without more context
 }
Run Code Online (Sandbox Code Playgroud)

我试过以下,

var button:UIButton = customerDashboardButtons!.first //Error "NSArray? is not convertible to UIButton"

var button = customerDashboardButtons!.first as UIButton //Error "NSArray? is not convertible to UIButton"
Run Code Online (Sandbox Code Playgroud)

对此有任何帮助表示赞赏.

mil*_*526 7

@IBOutlet var customerDashboardButtons:[NSArray]?
Run Code Online (Sandbox Code Playgroud)

创建一个数组数组.
呼叫customerDashboardButtons!.first将返回第一个阵列(该NSArray阵列中)(中[…]还将创建一个数组)

我怀疑你希望你customerDashboardButtons成为一个阵列,UIButton所以你会使用

@IBOutlet var customerDashboardButtons:[UIButton]?
Run Code Online (Sandbox Code Playgroud)

customerDashboardButtons!.first在这里使用会给你一个UIButton.由于数组的类型是UIButton,您不必将您声明button为UIButton.

  var button = customerDashboardButtons!.first
Run Code Online (Sandbox Code Playgroud)

如果您更改阵列将会工作.