将多个布局约束数组附加到单个Swift数组中?

时间:2014-11-20 13:32:47

标签: ios arrays swift autolayout

如何将多个constraintsWithVisualFormat:options:metrics:views:调用返回的布局约束收集到一个数组中?例如,Swift编译器将此标记为错误[NSLayoutConstraint] is not identical to 'UInt8'

var newConstraints: [NSLayoutConstraint] = []
newConstraints +=  NSLayoutConstraint.constraintsWithVisualFormat(format1, options: nil, metrics: nil, views: views)

我正在使用Xcode 6.1。

1 个答案:

答案 0 :(得分:1)

constraintsWithVisualFormat返回[AnyObject]。您需要将其[NSLayoutConstraint]强制转换为[NSLayoutConstraint]类型的数组:

newConstraints += NSLayoutConstraint.constraintsWithVisualFormat(format1, options: nil, metrics: nil, views: views) 
  as [NSLayoutConstraint]