How can I make setter to perform rounding of the width value during setting up the size.width using extension or inside Shape struct/setter without knowing letting caller to do it?
Struct Shape {
var size: CGSize = .zero
}
let shape1 = Shape()
shape1.size.width = 1.95
print(shape1.size.width) // 1.95
// Do not round here but in the Shape struct or extension
print(round(shape1.size.width)) // 2.0 is the expected answer
This might not be worth doing in this case, but I'll note that you can extract property observation/getter/setter behaviour into a property wrapper, so that it can have a name and be easily reused. Here's an example that does the rounding on every set