I have an auto layout convenience function which stacks views on top of each other. For my spacing parameter, I would like to allow:
CGFloat
(all views spaced evenly)CGFloat
s (different spaces between views)However, I'm not sure how to define this argument.
// Ambiguous
func myconstrainer(_ views : [UIView], spacing : CGFloat = 0){}
func myconstrainer(_ views : [UIView], spacing : [CGFloat]? = nil){}
// Messy
func myconstrainer(_ views : [UIView], spacingA : CGFloat = 0, spacingB: [CGFloat]? = nil){}
You can also try variable number of arguments like this:
func myconstrainer(_ views: [UIView], spacing: CGFloat...) {
myconstrainer(views, spacing: spacing)
}
and provide a simple overload to work with arrays as well:
func myconstrainer(_ views: [UIView], spacing: [CGFloat]) {
// Implementation...
}
As such, you won't need to put a single element in brackets when using even spacing.
Call sites:
myconstrainer([view1, view2, view3])
myconstrainer([view1, view2, view3], spacing: 123)
myconstrainer([view1, view2, view3], spacings: 1, 2, 3)
myconstrainer([view1, view2, view3], spacings: [1, 2, 3])
Some non-obvious notes:
CGFloat...
with zero arguments;