20

In Xcode 9 and Swift 4 I always get this warning for some IBInspectable properties:

    @IBDesignable public class CircularIndicator: UIView {
        // this has a warning
        @IBInspectable var backgroundIndicatorLineWidth: CGFloat? {  // <-- warning here
            didSet {
                backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
            }
        }

    // this doesn't have a warning
    @IBInspectable var topIndicatorFillColor: UIColor? {
        didSet {
            topIndicator.fillColor = topIndicatorFillColor?.cgColor
        }
    }
}

Is there a way to get rid of it ?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Adrian
  • 19,440
  • 34
  • 112
  • 219

3 Answers3

35

Maybe.

The exact error (not warning) I got when doing a copy/paste of class CircularIndicator: UIView is:

Property cannot be marked @IBInspectable because its type cannot be represented in Objective-C

I resolved it by making this change:

@IBInspectable var backgroundIndicatorLineWidth: CGFloat? {  // <-- warning here
    didSet {
        backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
    }
}

To:

@IBInspectable var backgroundIndicatorLineWidth: CGFloat = 0.0 {
    didSet {
        backgroundIndicator.lineWidth = backgroundIndicatorLineWidth
    }
}

Of course, backgroundIndicator is undefined in my project.

But if you are coding against didSet, it looks like you just need to define a default value instead of making backgroundIndicatorLineWidth optional.

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
  • I still get the warning, even after I remove the optional and add default value. – Adrian Sep 03 '17 at 14:43
  • Then there has to be something (besides a closing bracket) different between the code you posted and your project code. My *error* (which may be an indication) disappeared. I'll edit my answer with that code. –  Sep 03 '17 at 14:52
  • it's ok now, they are gone. I guess it took some time for Xcode to update – Adrian Sep 03 '17 at 14:55
  • Almost added the edit. Guess that's overwork. Glad I could help. –  Sep 03 '17 at 14:56
  • And what if I still need the optional property? :-) – Vitya Shurapov Oct 12 '17 at 13:00
  • 1
    I'd probably *try very hard* to not make an optional IB property (since `IBDesignable` is meant for *visible* properties, shouldn't they have a default?), but maybe try defaulting to `nil`? I'm not stating that such a thing would work, but it may be worth a shot. –  Oct 12 '17 at 14:32
9

Below two points might helps you

  1. As there is no concept of optional in objective c, So optional IBInspectable produces this error. I removed the optional and provided a default value.

  2. If you are using some enumerations types, then write @objc before that enum to remove this error.

Aaban Tariq Murtaza
  • 1,155
  • 14
  • 16
  • > As there is no concept of optional in objective c it is not exactly true. Classes is ObjC are nullable by design. UIColor is a class. So, correct equivalent of ObjC's `UIColor` in Swift is `UIColor?` – Krypt May 17 '19 at 18:21
5

Swift - 5

//Change this with below
@IBInspectable public var shadowPathRect: CGRect!{
    didSet {
        if shadowPathRect != oldValue {
            setNeedsDisplay()
        }
    }
}

To

@IBInspectable public var shadowPathRect: CGRect = CGRect(x:0, y:0, width:0, height:0) {
    didSet {
        if shadowPathRect != oldValue {
            setNeedsDisplay()
        }
    }
}
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34