IBInspectable Typing
ID |
swift.ibinspectable_typing |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Swift |
Tags |
ios, reliability, suspicious-code, ui |
Description
Reports an @IBInspectable property whose declared type is not in
the set Interface Builder can encode and present in the Attributes
inspector. The compiler accepts any type on an @IBInspectable
property, but only a fixed list of scalar types are wired through
to the design-time inspector. Anything else silently fails to
appear, leaving the property unsettable from the storyboard.
The supported types are:
-
Int,CGFloat,Double,Float -
Bool,String -
CGPoint,CGSize,CGRect -
UIColor,NSColor,UIImage,NSImage
Their T? / Optional<T> forms are also acceptable.
@IBInspectable var x: Int = 0 // OK
@IBInspectable var c: UIColor? // OK
@IBInspectable var arr: [Int] = [] // FLAW
@IBInspectable var dict: [String: Int] = [:] // FLAW
@IBInspectable var custom: MyType? // FLAW
Rationale
Marking a property @IBInspectable is a contract: the value will be
editable from the storyboard. Using an unsupported type breaks that
contract silently. The property compiles and the storyboard wiring
appears to succeed, but the value never reaches the runtime.
Remediation
Use one of the supported scalar types, or expose a supported-typed property that adapts to your richer model.
// Instead of an array, expose the canonical scalar:
@IBInspectable var cornerRadius: CGFloat = 0
// Instead of a custom struct, expose its components:
@IBInspectable var shadowColor: UIColor = .black
@IBInspectable var shadowOffsetX: CGFloat = 0
@IBInspectable var shadowOffsetY: CGFloat = 0