1

I have an array containing UIView subclass instances, and another array (of the same length) containing CGPoints. Without looping through the arrays, I'd like to assign the center property of each UIView subclass instance to the (index) associated CGPoint.

My first attempt looked something like UIViewArray.map {$0.center} = CGPointArray, but I quickly learned that map returns a copy of the array, so the assignment would be no good. The advice I've been able to find (e.g. this SO Q&A) only discusses using map for this sort of reassignment when the new parameter values are constant.

What's the best way to accomplish this task?

1
  • UIViewArray and CGPointArray are values, so they should be lowerCamelCase. UpperCamelCase is reserved for type names in Swift. These names don't really communicate anything that isn't obvious from the types. Consider perhaps views and centerPoints. Their array-ness is implied by the plurality, and the fact that it's CGPoint is already clear from the type Commented Nov 27, 2023 at 20:55

1 Answer 1

1

You can zip the two together, and mutate the UIViews with either a for loop or a forEach call.

zip(views, centerPoints).forEach { $0.center = $1 }
Sign up to request clarification or add additional context in comments.

Comments

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.