How to set maximum limit for IBInspectable Int

I use IBInspectable Int in Swift to select between four four forms (0-3), however in the storyboard editor you can set a value greater than 3 and less than 0, which stops IBDesignable from working.

Can I set a minimum and maximum limit on what values ​​can be set in the storyboard editor?

let SHAPE_CROSS = 0 let SHAPE_SQUARE = 1 let SHAPE_CIRCLE = 2 let SHAPE_TRIANGLE = 3 @IBInspectable var shapeType: Int = 0 @IBInspectable var shapeSize: CGFloat = 100.0 @IBInspectable var shapeColor: UIColor? 
+6
source share
1 answer

It is not possible to limit what the user can enter into the storyboard. However, you can prevent the storage of invalid values ​​using a computed property:

  @IBInspectable var shapeType: Int { set(newValue) { internalShapeType = min(newValue, 3) } get { return internalShapeType } } var internalShapeType: Int = 0 

Then you can also use enum instead of constants to represent different types of shapes inside.

+8
source

Source: https://habr.com/ru/post/989096/


All Articles