In Obj-C, a common practice was to use convenience functions to perform common operations, like configuring auto layout for views:
func makeConstraint(withAnotherView : UIView) -> NSLayoutConstraint
{
// Make some constraint
// ...
// Return the created constraint
return NSLayoutConstraint()
}
If you just needed to set the constraint and forget about it, you could call:
[view1 makeConstraint: view2]
If you wanted to store the constraint later so that you could remove/modify it, you would do something like:
NSLayoutConstraint * c;
c = [view1 makeConstraint: view2]
I want to do this in swift, but if I call the above function and do not capture the returned constraint, I get the warning:
Result of call to 'makeConstraint(withAnotherView:)' is unused
VERY annoying. Is there some way to let Swift know that I don't always want to capture the return value?
NOTE: I know about this. It is ugly and not what I'm looking for:
_ = view1.makeConstraint(withAnotherView: view2)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…