1

Is't there a way set multiple variables equal to same value in Swift? like:

self.view1.backgroundColor = .white
self.view2.backgroundColor = .white
self.view3.backgroundColor = .white

make it into one line

self.view1.backgroundColor, self.view2.backgroundColor, self.view3.backgroundColor = .white

1 Answer 1

8

Maybe something like this, if you must use a one-liner:

[view1, view2, view3].forEach { $0.backgroundColor = .white }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. can self.label.textColor = .white self.view2.backgroundColor = .white use this way?
@0x0 you could add a ;, so self.label.textColor = .white; self.view2.backgroundColor = .white. But just split it into 2 lines instead... try to avoid ; in Swift.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.