-1

I'm trying to declare a constant that should have a value that is calculated using the frame property of UIView. Of course, I cannot access this property when declaring the constant outside of draw(), and I'm trying to find a workaround.

I've read about lazy variables, but I'm not sure if it is the best option. Also, lazy variables cannot be constants.

Should I just change my constant to a lazy var or is there another way?

Xcoder
  • 1,433
  • 3
  • 17
  • 37

1 Answers1

0

If you have something computed from a frame then it is by definition not a constant, since 1) frame only has a valid value after the layout pass and 2) frame can actually change based on the orientation of the device, multitasking, and the context in which the view is presented. You should use an optional variable or one with a default value for the case in which frame has yet to be set, depending on what's more appropriate for you use case. You can use lazy if you can guarantee that no-one is going to call that property until after layout has occurred.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33