How to set a max limit for an IBInspectable Int

后端 未结 1 871
温柔的废话
温柔的废话 2021-01-13 00:01

I am using an IBInspectable Int in Swift to choose between 4 four shapes (0-3), however it is possible in the storyboard editor to set a value greater than 3 and less than 0

1条回答
  •  日久生厌
    2021-01-13 00:56

    There's no way to limit what a user can input in Storyboard. However, you could prevent invalid values from being stored using a computed property:

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

    Then you could also use an enum instead of constants to represent your different shape types internally.

    0 讨论(0)
提交回复
热议问题