1

我收到一个错误,因为“表达式类型不明确,没有更多上下文”

这是我的代码

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

我尝试了以下方法,

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"

对此的任何帮助表示赞赏。

4

2 回答 2

7
@IBOutlet var customerDashboardButtons:[NSArray]?

创建一个数组数组。
调用customerDashboardButtons!.first将返回数组中的第一个数组 (the NSArray) (the[…]还将创建一个数组)

我怀疑你希望你customerDashboardButtons是一个数组,UIButton所以你会使用

@IBOutlet var customerDashboardButtons:[UIButton]?

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

  var button = customerDashboardButtons!.first

如果您更改阵列,它将起作用。

于 2015-05-26T08:31:01.087 回答
0

这是一个如何创建 IBOutletCollection 并从中获取项目的示例

1) @IBOutlet var simpleTextField: [UITextField]!

2) 从中获取 textFiled 的示例。

for index in 0...(simpleTextField.count - 1)  {
    let textField : UITextField = simpleTextField[index]
    textField.isUserInteractionEnabled = true
}
于 2017-04-29T06:38:00.620 回答