Swift

Swift

Made by DeepSource

@IBInspectable should be applied to variables only, have its type explicit and be of a supported type SW-W1006

Bug risk
Major

The @IBInspectable attribute in Swift is used to expose properties to Interface Builder, allowing them to be configured visually. However, there are certain guidelines and restrictions that should be followed when using this attribute.

The issue arises when the @IBInspectable attribute is applied to something other than a variable, or when the type of the variable is not explicit or not one of the supported types (boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil). This can lead to unexpected behavior or build errors when working with Interface Builder, as it relies on the type information provided by the attribute.

Bad Practice

class CustomView: UIView {
  @IBInspectable func foo() {} // Incorrect: @IBInspectable should be applied to variables only
  @IBInspectable var bar: Int // Incorrect: Type is not explicitly specified
  @IBInspectable var baz: URL // Incorrect: Unsupported type
}

Recommended

class CustomView: UIView {
  @IBInspectable var backgroundColor: UIColor // Correct: Applying @IBInspectable to a variable
  @IBInspectable var borderWidth: CGFloat // Correct: Type is explicitly specified
  @IBInspectable var isRounded: Bool // Correct: Using a supported data type
}