当前位置: 动力学知识库 > 问答 > 编程问答 >

swift - create variable and add to array in same line

问题描述:

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.

网友答案:

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>))
分享给朋友:
您可能感兴趣的文章:
随机阅读: