0

I have created a bunch of views and sliders in my page and as a result I have quite a few constraint constants. My friend noticed that after each group I would use

NSLayoutConstraint.activate([...,...,...])

and then for the next set of items I would do the same. He suggested that I just create an array like this

var allConstraints = [NSLayoutConstraint]()

and append each constraint to the array and then do it all at once

so after each constant that I create I am saying

allConstraints.append(...)

What I am curious to know is when I create the NSLayoutConstraint constant can I, in the same line, automatically add it to allConstraints array?

That seems like it would be cleaner.

1 Answer 1

0

doing so will make your code hard to read so i suggest you don't! but yes you can achieve this in a single line

allConstraints.append(NSLayoutConstraint(item: AnyObject>, attribute: <NSLayoutAttribute>, relatedBy: <NSLayoutRelation>, toItem: <AnyObject?>, attribute: <NSLayoutAttribute>, multiplier: <CGFloat>, constant: <CGFloat>))
Sign up to request clarification or add additional context in comments.

2 Comments

Do you recommend just saying....... allConstraints+=[xxx,yyy]....at the bottom of all of them?
e.g you have two constraints, let heightConstraint = NSLayoutConstraint() let widthConstraint = NSLayoutConstraint() allConstraints.append(contentsOf: [heightConstraint, widthConstraint])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.