如何设置IBInspectable Int的最大限制 [英] How to set a max limit for an IBInspectable Int
问题描述
我在Swift中使用IBInspectable Int来选择4种形状(0-3),但在故事板编辑器中可以设置大于3且小于0的值,这会阻止IBDesignable系统工作。
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, which stops the IBDesignable system working.
是否可以设置故事板编辑器中可以设置的最小值和最大值?
Is it possible to set a min and max limit of 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?
推荐答案
没有办法限制用户输入的内容故事板。但是,您可以使用计算属性阻止无效值的存储:
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.
这篇关于如何设置IBInspectable Int的最大限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!