我在我的页面中创建了一堆 View 和 slider ,因此我有很多约束常量。我的 friend 注意到在每组之后我会使用
NSLayoutConstraint.activate([...,...,...])
然后对于下一组项目,我会做同样的事情。 他建议我只创建一个这样的数组
var allConstraints = [NSLayoutConstraint]()
并将每个约束追加到数组中,然后一次性完成
所以在我创建的每个常量之后我都在说
allConstraints.append(...)
我想知道的是,当我创建 NSLayoutConstraint 常量时,我能否在同一行中自动将其添加到 allConstraints 数组?
这样看起来会更干净。
最佳答案
这样做会使您的代码难以阅读,所以我建议您不要这么做!但是是的,你可以在一行中实现这一点
allConstraints.append(NSLayoutConstraint(item: AnyObject>, attribute: <NSLayoutAttribute>, relatedBy: <NSLayoutRelation>, toItem: <AnyObject?>, attribute: <NSLayoutAttribute>, multiplier: <CGFloat>, constant: <CGFloat>))
关于swift - 在同一行创建变量并添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41012802/